iOS 11-使用大标题模式时,UINavigationItem titleView [英] iOS 11 - UINavigationItem titleView when using Large Titles mode

查看:447
本文介绍了iOS 11-使用大标题模式时,UINavigationItem titleView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解它是错误还是预期的行为.

I'm trying to understand either it's a bug or it's the expected behavior.

iOS 10 及更早版本上,我们可以使用navigationItem.titleView设置自定义标题.
iOS 11 上,设置我们的navigationItem.largeTitleDisplayMode = .always和设置navigationItem.titleView = <Some cool UIButton>时,它同时显示 和正常的导航标题栏以及较大的导航标题.

On iOS 10 and earlier we could set up a custom title, using navigationItem.titleView.
On iOS 11, when setting our navigationItem.largeTitleDisplayMode = .always and setting navigationItem.titleView = <Some cool UIButton> it's displaying both the normal navigation title bar and the large navigation title.

插图:

总结:

如何在大型导航标题上使用自定义titleView?

这是预期的结果:

推荐答案

通过使用UINavigationBar的子类并手动更改视图层次结构,我能够用自定义视图替换导航栏大标题:

I was able to replace the navigation bar big title with a custom view by using a subclass of UINavigationBar and manually changing the view hierarchy :

@interface MYNavigationBar : UINavigationBar

@end

@implementation MYNavigationBar

// ...

- (void)layoutIfNeeded
{
    [self setupTitle];
    [super layoutIfNeeded];
}

- (void)setupTitle
{
    // UINavigationBar
    // -> ...
    // -> _UINavigationBarLargeTitleView
    //   -> UILabel << Big Title Label
    //   -> UIView
    //     -> UILabel << Big Title Label animating from back button during transitions

    for (UIView *view in self.subviews) {
        NSString *className = NSStringFromClass(view.classForCoder);
        if ([className containsString:@"LargeTitleView"]) {
            for (UIView *view2 in view.subviews) {
                if ([view2 isKindOfClass:[UILabel class]]) {
                    [self convertLabel:(UILabel *)view2];
                }
                for (UIView *view3 in view2.subviews) {
                    if ([view3 isKindOfClass:[UILabel class]]) {
                        [self convertLabel:(UILabel *)view3];
                    }
                }
            }
        }
    }
}

- (void)convertLabel:(UILabel*)label
{
    // I kept the original label in the hierarchy (with the background color as text color)
    // and added my custom view as a subview. 
    // This allow the transformations applied to the original label to be also applied to mine.
}

请注意,由于这种技巧,苹果可能拒绝了您的应用.

Please note that Apple might reject your app because of this trick.

这篇关于iOS 11-使用大标题模式时,UINavigationItem titleView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆