iPhone - 整个堆栈的自定义 UINavigation 栏

发布于 2024-11-04 21:39:49 字数 1704 浏览 0 评论 0原文

我想在 UINavigationBar 的中心以及所有导航堆栈中设置一个图像和一个标签。

我目前正在做的是将其添加到我的导航项 titleView 中。 这种方法的“问题”是我必须在 viewDidLoad 中为推送到导航堆栈的每个视图控制器调用此方法。 另一种方法是将 UILable 和 UIImageView 直接添加到 UINavigationBar 中,但这就是为什么我必须自己计算中心,此外我读到这不是推荐的方法。 知道如何得到我想要的东西吗?

我的代码:

    CGRect  navTitle = controller.navigationController.navigationBar.bounds;
    CGFloat aHeight = navTitle.size.height;

    UIView* container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 163, aHeight)];


    UIImage* statusImg = [UIUtils   getStatusImage]; 

    UIImageView *statusImage = [[UIImageView alloc] initWithFrame:CGRectMake(0,aHeight/2-statusImg.size.height/2, 33., 32.)];
    statusImage.autoresizingMask = UIViewAutoresizingNone;
    statusImage.image = statusImg;
    statusImage.backgroundColor = [UIColor clearColor];
    [statusImage setTag:1];
    [statusImage setHidden:NO];

initWithFrame:CGRectMake(statusImage.frame.origin.x + 33. + 3, 0, 130., navTitle.size.height)];

    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(statusImage.frame.origin.x + 33. + 3, 0, 130., navTitle.size.height)];
    titleLabel.textAlignment = UITextAlignmentLeft;
    titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont boldSystemFontOfSize:20.];
    titleLabel.shadowOffset = CGSizeMake(1, -1);
    titleLabel.opaque = NO;
    titleLabel.backgroundColor = [UIColor clearColor];
    [titleLabel setTag:2];

    [container addSubview:statusImage];
    [container addSubview:titleLabel];
    controller.navigationItem.titleView = container;
[statusImage release];
[titleLabel release];
    [container release];

I want to set an image and a label at the center of my UINavigationBar, along all my navigation stack.

What I'm currently doing is adding it to my navigation item titleView.
The "problem" with this approach is that I have to call this method in the viewDidLoad for each view controller I push to my navigation stack.
The other way around is to add the UILable and UIImageView directly to the UINavigationBar, however that why I have to calculate the center myself, and in addition I read that's not the recommended approach.
Any Idea how to get what I want ?

My Code:

    CGRect  navTitle = controller.navigationController.navigationBar.bounds;
    CGFloat aHeight = navTitle.size.height;

    UIView* container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 163, aHeight)];


    UIImage* statusImg = [UIUtils   getStatusImage]; 

    UIImageView *statusImage = [[UIImageView alloc] initWithFrame:CGRectMake(0,aHeight/2-statusImg.size.height/2, 33., 32.)];
    statusImage.autoresizingMask = UIViewAutoresizingNone;
    statusImage.image = statusImg;
    statusImage.backgroundColor = [UIColor clearColor];
    [statusImage setTag:1];
    [statusImage setHidden:NO];

initWithFrame:CGRectMake(statusImage.frame.origin.x + 33. + 3, 0, 130., navTitle.size.height)];

    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(statusImage.frame.origin.x + 33. + 3, 0, 130., navTitle.size.height)];
    titleLabel.textAlignment = UITextAlignmentLeft;
    titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont boldSystemFontOfSize:20.];
    titleLabel.shadowOffset = CGSizeMake(1, -1);
    titleLabel.opaque = NO;
    titleLabel.backgroundColor = [UIColor clearColor];
    [titleLabel setTag:2];

    [container addSubview:statusImage];
    [container addSubview:titleLabel];
    controller.navigationItem.titleView = container;
[statusImage release];
[titleLabel release];
    [container release];

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

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

发布评论

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

评论(1

毁梦 2024-11-11 21:39:49

找到了一个很好的方法:

将自己注册为 UINavigationController 的委托将让您在每次即将推送新控制器时收到回调。

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated

在该函数中,获取 viewController 并对其导航项进行操作即可实现此目的。

Found the a nice way to do it :

Registering yourself as the delegate of UINavigationController will let you receive a callback each time a new controller is about to be pushed.

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated

Inside that function, getting the viewController and operating on his navigationitem will do the trick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文