【问题标题】:iOS 11.2 custom navigation bar infinite loopiOS 11.2 自定义导航栏无限循环
【发布时间】:2017-12-20 11:35:50
【问题描述】:

自 IOS 11.2 发布以来,我的应用在推送其导航控制器具有自定义导航栏高度的视图控制器时遇到了无限循环。有人找到解决这个问题的方法了吗?谢谢。

-(void)layoutSubviews{
   [super layoutSubviews];
   float height = 42.5;

   if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
       height = 48;

   }

   imageView.frame = CGRectMake(0, 0, [[UIScreen mainScreen]bounds].size.width, height);
   if(UI_USER_INTERFACE_IDIOM() ==UIUserInterfaceIdiomPad){

       if(@available(iOS 11.0,*)){
           self.frame =CGRectMake(0, 20,[[UIScreen mainScreen]bounds].size.width, 55); // this line provoke the infinite loop

           for(UIView *aView in self.subviews){
               if([NSStringFromClass([aView class]) isEqualToString: @"_UINavigationBarContentView"]){
                   aView.frame = CGRectMake(0, 10, aView.frame.size.width, aView.frame.size.height);
               }
               if([NSStringFromClass([aView class]) isEqualToString: @"_UIBarBackground"]){
                   aView.frame = CGRectMake(0, 0, aView.frame.size.width, self.frame.size.height );
               }

           }

       }
   }
}

【问题讨论】:

  • 您在哪里发现代码中的无限循环?请提及那行代码。
  • self.frame =CGRectMake(0, 20,[[UIScreen mainScreen]bounds].size.width, 55);这一行引发了无限循环
  • 好的。这是 UINavigationBar 的子类,您如何添加自定义导航栏?

标签: ios objective-c uinavigationcontroller uinavigationbar ios11.2


【解决方案1】:

我也遇到了同样的问题。
我通过删除这个解决了

frame.size.height = customHeight

从 layoutSubviews 然后添加这个

override var frame: CGRect {

    get {

        return CGRect(x: 0, y: 0, width: super.frame.width, height: customHeight)

    }
    set (value) {
        super.frame = value

    }

}

到我的 UINavigationBar 子类。
我像以前一样将所有其他代码留在 layoutSubviews 中。 (注)

【讨论】:

  • 这修复了无限循环,但导航栏框架不会改变。
猜你喜欢
  • 1970-01-01
  • 2011-09-04
  • 2015-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-09
  • 2017-11-07
  • 1970-01-01
相关资源
最近更新 更多