淡入/淡出导航控制器标题

发布于 2024-10-21 05:32:42 字数 424 浏览 2 评论 0原文

我已经使用 title 属性设置了导航控制器的标题。我希望标题能够淡入和淡出以响应用户执行的某些操作。

我当前的解决方案是为导航控制器提供自定义 titleView,并且每当我希望标题淡出时,我都会为该视图的 alpha 变化设置动画。这有效,但产生了另一个问题:我无法居中对齐标题,因为左栏项目和右栏项目的宽度不同。

因此,我正在寻找替代解决方案。是否有另一种方法可以在不使用自定义标题视图的情况下淡化导航控制器标题?

编辑:我收到了一些答案,展示了如何通过添加子视图来做到这一点。尽管这确实启用了我想要的淡入/淡出功能,但我想避免使用子视图,因为我在实现该解决方案时遇到了问题。

导航对象中是否内置了允许标题淡入淡出的内容?我已经做了一些研究,从迄今为止的反应来看,我猜还没有。我只是作为最后的手段发帖,以防万一有人知道一种方法。

I have set my navigation controller's title with the title property. I would like the title to be able to fade in and out in response to some action performed by the user.

My current solution is to provide the navigation controller with a custom titleView, and I would animate the alpha change of that view whenever I want the title to fade. This works, but is creating another problem: I cannot center align the title since the left bar item and the right bar item are varied in width.

Therefore, I am looking for an alternate solution. Is there another way to fade the navigation controller title without using a custom title view?

EDIT: I have received some answers showing how to do this by adding subviews. Although that does enable the fade in/out feature I want, I would like to avoid using subviews as I ran into a problem when implementing that solution.

Is there something built into the navigation objects that allows the fading of the title? I have already done some research, and gauging form the responses thus far, I'm guessing there isn't. I'm just posting as a last resort, just incase anyone knows of a way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

全部不再 2024-10-28 05:32:42

同意 @ImaginaryCake 的评论:调整 title 属性听起来像是黑客。正确的方法是使用 titleView 而不是迭代 UINavitationBar -subviews 并希望它能够在 SDK 更新后继续存在。

快速&只需 2 步即可清洁;在 UIViewController 中,执行以下操作:

第 1 步:更改 navigationItemtitleView

self.navigationItem.titleView = ({
    UILabel * titleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
    [titleView setTextAlignment:NSTextAlignmentCenter];
    [titleView setText:@"HeyHey"];

    titleView;
});

第 2 步:随意制作动画。

[UIView animateWithDuration:1.0f animations:^{
    self.navigationItem.titleView.alpha = 0.5f;
}];

Agreed with @ImaginaryCake comment: Tweaking the title property sounds like a hack. The proper way is to use the titleView instead of iterating through the UINavitationBar -subviews and hope it will survive SDK updates.

Quick & clean in only 2 steps ; From a UIViewController, do:

Step 1: Change the titleView for your navigationItem.

self.navigationItem.titleView = ({
    UILabel * titleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
    [titleView setTextAlignment:NSTextAlignmentCenter];
    [titleView setText:@"HeyHey"];

    titleView;
});

Step 2: Animate at will.

[UIView animateWithDuration:1.0f animations:^{
    self.navigationItem.titleView.alpha = 0.5f;
}];
汹涌人海 2024-10-28 05:32:42

它可能对你有用......

fade.h

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController
 {

     UINavigationBar *navigationbar;

     NSTimer * timer;
     UILabel * ll;
      UILabel * l2;
 }

@property(nonatomic,retain) UINavigationBar *navigationbar;


@end

fade.m

#define FADE_IN_RATE        2.0/100.0
#define FADE_OUT_RATE       2.0/200.0
- (void)viewDidLoad {   
    [super viewDidLoad];
    ll.alpha = 0.0;
    l2.alpha =1.0;
    ll = [[UILabel alloc]init];
    l2 = [[UILabel alloc]init];
    l2.backgroundColor = [UIColor grayColor];
    ll.text = @"Name";
    ll.backgroundColor = [UIColor grayColor];
    ll.frame = CGRectMake(115, 10, 60, 20);
    l2.frame = CGRectMake(115, 10, 60, 20);
    [self.navigationController.navigationBar addSubview:ll];
    [self.navigationController.navigationBar addSubview:l2];
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    timer = [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(fadeScreen) userInfo:nil repeats:NO];
}
- (void)fadeScreen
{

    timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(fadeTimerEvent) userInfo:nil repeats:YES];


}

- (void)fadeTimerEvent
{

    if (ll.alpha >= 2)
    {
                // At this point, layer1 is now visible  
        [timer invalidate];
        timer = nil;    

    }
    else
    { 

        // Fade lower layer in (increase alpha)
         ll.alpha += FADE_IN_RATE;

        // Fade upper layer out (decrease alpha)
          l2.alpha -= FADE_OUT_RATE;
    }
}

问候,
CN西瓦库玛

It may be useful for u....

fade.h

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController
 {

     UINavigationBar *navigationbar;

     NSTimer * timer;
     UILabel * ll;
      UILabel * l2;
 }

@property(nonatomic,retain) UINavigationBar *navigationbar;


@end

fade.m

#define FADE_IN_RATE        2.0/100.0
#define FADE_OUT_RATE       2.0/200.0
- (void)viewDidLoad {   
    [super viewDidLoad];
    ll.alpha = 0.0;
    l2.alpha =1.0;
    ll = [[UILabel alloc]init];
    l2 = [[UILabel alloc]init];
    l2.backgroundColor = [UIColor grayColor];
    ll.text = @"Name";
    ll.backgroundColor = [UIColor grayColor];
    ll.frame = CGRectMake(115, 10, 60, 20);
    l2.frame = CGRectMake(115, 10, 60, 20);
    [self.navigationController.navigationBar addSubview:ll];
    [self.navigationController.navigationBar addSubview:l2];
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    timer = [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(fadeScreen) userInfo:nil repeats:NO];
}
- (void)fadeScreen
{

    timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(fadeTimerEvent) userInfo:nil repeats:YES];


}

- (void)fadeTimerEvent
{

    if (ll.alpha >= 2)
    {
                // At this point, layer1 is now visible  
        [timer invalidate];
        timer = nil;    

    }
    else
    { 

        // Fade lower layer in (increase alpha)
         ll.alpha += FADE_IN_RATE;

        // Fade upper layer out (decrease alpha)
          l2.alpha -= FADE_OUT_RATE;
    }
}

Regards,
CNSivakumar

羁〃客ぐ 2024-10-28 05:32:42

UIViewControllertitle 属性只是一个 NSString。您需要将自定义视图添加到导航栏。

标题

代表的本地化字符串
查看该控制器管理的视图。

@property(非原子,复制) NSString
*标题

讨论

子类应将标题设置为
人类可读的字符串表示
用户的视图。如果接收者
是一个导航控制器,
默认值为顶视图
控制器的标题。

The title property of UIViewController is just a NSString. You need to add your custom view to your NavigationBar.

title

A localized string that represents the
view that this controller manages.

@property(nonatomic, copy) NSString
*title

Discussion

Subclasses should set the title to a
human-readable string that represents
the view to the user. If the receiver
is a navigation controller, the
default value is the top view
controller’s title.

在你怀里撒娇 2024-10-28 05:32:42

快速而肮脏:您应该能够通过以下方式将自定义标签添加到导航栏:

[navigationBar addSubview:yourLabel];

只需将标签的框架设置为在导航栏中居中,并像以前一样修改其 alpha 值。然而,这是一个真正的黑客,我不确定当您推送和弹出视图控制器时它的行为如何。

编辑:

好的,给你。首先假设我们有一个导航栏,其中包含具有 title 属性(不是 titleView 属性)的导航项以及左侧和右侧栏按钮项集。

UINavigationBar* bar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[self.view addSubview:bar];

UINavigationItem* anItem = [[UINavigationItem alloc] initWithTitle:@"HeyHey"];
UIBarButtonItem* leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:nil action:nil];
UIBarButtonItem* rightItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:nil action:nil];
anItem.leftBarButtonItem = leftItem;
anItem.rightBarButtonItem = rightItem;
bar.items = [NSArray arrayWithObject:anItem];
[leftItem release];
[rightItem release];
[anItem release];

现在您可以像这样淡出标题视图:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[[[bar subviews] objectAtIndex:0] setAlpha:0];
[UIView commitAnimations];

我不知道标题视图是否始终位于索引 0,但您可以使用检查您的特定情况

for (UIView* aView in bar.subviews) {
    NSLog(@"%@", aView);
}

对于标题视图,您将看到类似的内容

<UINavigationItemView: 0x7566530; frame = (0 0; 0 0); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x75665e0>>

Quick and dirty: You should be able to add a custom label to the navigation bar via

[navigationBar addSubview:yourLabel];

Just setup the label's frame to be centered within the navigation bar and modify its alpha value as you did before. However, this is a real hack and I'm not sure how it behaves when you're pushing and popping view controllers.

EDIT:

Ok, here you go. First assume we have a navigation bar holding a navigation item with the title property (not the titleView property) as well as the left and right bar button item set.

UINavigationBar* bar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[self.view addSubview:bar];

UINavigationItem* anItem = [[UINavigationItem alloc] initWithTitle:@"HeyHey"];
UIBarButtonItem* leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:nil action:nil];
UIBarButtonItem* rightItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:nil action:nil];
anItem.leftBarButtonItem = leftItem;
anItem.rightBarButtonItem = rightItem;
bar.items = [NSArray arrayWithObject:anItem];
[leftItem release];
[rightItem release];
[anItem release];

Now you can fade the title view like so:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[[[bar subviews] objectAtIndex:0] setAlpha:0];
[UIView commitAnimations];

I don't know if the title view is always at index 0, but you can check for your particular case using

for (UIView* aView in bar.subviews) {
    NSLog(@"%@", aView);
}

For the title view you will see something like

<UINavigationItemView: 0x7566530; frame = (0 0; 0 0); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x75665e0>>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文