如何在iOS 11中正确设置UINavigationBar高度 [英] How to correctly set UINavigationBar height in iOS 11

查看:1225
本文介绍了如何在iOS 11中正确设置UINavigationBar高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的iOS 9+应用程序中,我在一些 UIViewController 中手动使用 UINavigationBar (=放置它们直接在VC中而不是使用 UINavigationViewController )。

In my iOS 9+ app I use UINavigationBar in some of the UIViewControllers "manually" (= placing them directly in the VC instead of using UINavigationViewController).

为这样的 UINavigationBar 使用简单的约束是没有问题的。但是在iOS 11中,条形本身仍然正确使用指定的大小,但内容不再正确定位(现在已分配到顶部)。

Setting a height for such a UINavigationBar using a simple contraint was no problem before. However in iOS 11 the bar itself still correctly uses the assigned size, but the content is not positioned correctly any more (is now alligned to the top).

任何想法如何解决这个?

编辑:

我很确定,这不是重复的自定义iOS 11导航栏高度自定义,这个问题解决了一个问题 UINavigationBar 我直接使用 UINavigationBar 的子类。此外,自Beta 4以来所描述的问题似乎已经解决了,而我在Beta 6中遇到了问题。

I am pretty sure, that this is not a duplicate of "customizing iOS 11 navigation bar height customizing", this this question addresses a problem with UINavigationBar subclasses while I directly use UINavigationBar. Additionally the described problem seems to solved since Beta 4, while I am experiencing the problem in Beta 6.

编辑2:

同时我实现了 CharlieSu在另一个中提出的 UINavigationBar 子类线程。然而,这并没有解决我的问题。似乎子视图框都已正确设置(无需手动执行),但无论如何都要打破布局。因此手动设置框架没有任何区别。

Meanwhile I implemented a UINavigationBar subclass as proposed by CharlieSu in the other thread. However this did not solve the problem in my case. It seems that the subview frame are all set properly (without doing this manually), but the layout is broken anyway. So setting the frames manually does not make any difference.

推荐答案

我也遇到过这个问题。我这样解决了:

I've had this problem, too. I solved it this way:

 -(void)layoutSubviews{
        [super layoutSubviews];
        CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
        if (rectStatus.size.height==44.f) {

        }else{
            if (@available(iOS 11.0, *)) {
                for ( UIView*aView in self.subviews) {
                    if ([NSStringFromClass(aView.classForCoder) isEqualToString:@"_UINavigationBarContentView"]) {
                        aView.frame = CGRectMake( 0,20,aView.frame.size.width,44);
                    }
                    else if ([NSStringFromClass(aView.classForCoder) isEqualToString:@"_UIBarBackground"]) {
                        aView.frame = CGRectMake(0,0,aView.frame.size.width, 64);
                    }
                }
            }
        }
    }

这篇关于如何在iOS 11中正确设置UINavigationBar高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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