iOS解决导航引起视图高度问题

经过导航栏跨越的坑,总结出有两种方法可以无痕解决(前提>=iOS7版本)(TabBar与导航栏类似)

1、通过设置导航栏的透明度实现(这种方式的控制器view的起始坐标是充(0,64)开始的)

(1)OC实现

  self.navigationBar.translucent = NO;

(2)swift实现

  navigationBar.isTranslucent = false

2、通过控制器edgesForExtendedLayout属性实现(这种方式的控制器view的起始坐标是充(0,0)开始的),这种方式可以实现导航栏的透明,也可以让导航栏不透明,具体设置可以根据需求而定,实现透明化用步骤1的代码即可

如果要实现导航栏和TabBar透明效果下面代码不能设置

(1)OC实现

  self.edgesForExtendedLayout = UIRectEdgeNone;

(2)swift实现

  edgesForExtendedLayout = .init(rawValue: 0)

3、translucent用法

iOS7之后由于navigationBar.translucent默认是YES,
原点在(0,0)点
当设置NO的时候,原点坐标在(0,64)点

// 原点从(0,64)开始

self.navigationController.navigationBar.translucent = NO;

5、automaticallyAdjustsScrollViewInsets用法

在用的时候都会有两种情况咯

1:单独设置self.automaticallyAdjustsScrollViewInsets

// 原点从(0,64)开始
self.automaticallyAdjustsScrollViewInsets = NO;

2:单独self.automaticallyAdjustsScrollViewInsets = NO设置,原点就是(0,0)开始

// 原点从(0,0)开始
self.automaticallyAdjustsScrollViewInsets = NO;

3:和self.edgesForExtendedLayout联合设置,原点就是(0,64)开始

// 原点从(0,64)开始
self.automaticallyAdjustsScrollViewInsets = NO;
self.edgesForExtendedLayout = UIRectEdgeNone;

系统就会自动根据UINavigationBar和statusBar将view下移64,frame从(0,64)开始。这样,我们在布局内部控件的时候依然可以从(0,0)开始,而不必担心上部被UINavigationBar遮挡了

3、autoResizingMask的作用

autoResizingMask 是UIView的一个属性,在一些简单的布局中,使用autoResizingMask,可以实现子控件相对于父控件的自动布局。

autoResizingMask 是UIViewAutoresizing 类型的,其定义为:

@property(nonatomic) UIViewAutoresizing autoresizingMask;    // simple resize. default is UIViewAutoresizingNone

UIViewAutoresizing 是一个枚举类型,默认是 UIViewAutoresizingNone,其可以取得值有:

复制代码
typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
    UIViewAutoresizingNone                 = 0,
    UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
    UIViewAutoresizingFlexibleWidth        = 1 << 1,
    UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
    UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
    UIViewAutoresizingFlexibleHeight       = 1 << 4,
    UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
复制代码

各属性解释:

UIViewAutoresizingNone

不会随父视图的改变而改变

UIViewAutoresizingFlexibleLeftMargin

自动调整view与父视图左边距,以保证右边距不变

UIViewAutoresizingFlexibleWidth

自动调整view的宽度,保证左边距和右边距不变

UIViewAutoresizingFlexibleRightMargin

自动调整view与父视图右边距,以保证左边距不变

UIViewAutoresizingFlexibleTopMargin

自动调整view与父视图上边距,以保证下边距不变

UIViewAutoresizingFlexibleHeight

自动调整view的高度,以保证上边距和下边距不变

UIViewAutoresizingFlexibleBottomMargin

自动调整view与父视图的下边距,以保证上边距不变

原文地址:https://www.cnblogs.com/yang-shuai/p/7483466.html