45

iOS 导航栏64的问题总结

 5 years ago
source link: http://www.cocoachina.com/ios/20180725/24298.html?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

首先我们默认加载一个tableView,看一下他的显示效果。

uY7N3iM.png!web

image.png

再看层级:

IZVbm2M.png!web image.png

我们可以看到tableview是从(0,0)坐标开始的但是内容是从(0,64)开始的。这是为什么呢?控制这个现象的属性又是什么呢?

这时我们需要用到相关属性:

当升级到iOS 11的时候,发现UIScrollView 有莫名其妙的偏移了 可是明明设置了automaticallyAdjustsScrollViewInsets

这是因为iOS 11为UIScrollView 添加了新的属性contentInsetAdjustmentBehavior 这是一个枚举

找到UIScrollViewContentInsetAdjustmentNever 从来不自动调整和automaticallyAdjustsScrollViewInsets = NO 是一个功效 这样就解决了iOS 11这个bug

if (@available(iOS 11.0, *)) {
  Scrollview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
            // Fallback on earlier versions
  self.automaticallyAdjustsScrollViewInsets = NO
}

我们看一下图层添加这两个属性后,图层变成什么样子了:

zqiqQfB.png!web image.png 6VzaIzZ.png!web image.png

我们看到tableview的frame并没有变化依然是从屏幕的(0,0)开始,但是内容的偏移发生了变化,也是从(0,0)开始。这个时候我们想让tableview正常显示就需要对tableview的frame进行改变(0,64),除了这个还有其他办法吗?

  self.edgesForExtendedLayout = UIRectEdgeNone;
  self.navigationController.navigationBar.translucent = NO;

这两个属性的性质是一样的,vc默认是全屏布局的,当你添加这个两个中任何一个后,vc的布局就变成从(0,64)布局,我们可以看一下图层:

byaYRnn.png!web image.png aymeiij.png!web image.png 以上只是我在遇到64偏移的时候,解决问题常用到的记得属性,在这里做了一下整理, 另外以上(0,64)中的64值得是导航栏的高度,注意iPhone X的适配


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK