IOS7屏幕适配(一)
原因:IOS7之前的版本中UIViewController中的view在显示后会自动调整为去掉导航栏的高度的,控件会自动在导航栏以下摆放,IOS7之后所有的UIViewController创建后默认就是full Screen的,因此如果带导航栏的应用界面中的部分控件会被导航栏覆盖掉。
(一)第一部分
1、适配方法:创建category
#import "UIViewController+AddEdgesForExtendedLayout.h" @implementation UIViewController (AddEdgesForExtendedLayout) +(void)addEdgesForExtendedLayout:(UIViewController *)contoller { #ifdef __IPHONE_7_0 if ([[UIDevice currentDevice].systemVersion floatValue] >= 7.0) { if ([contoller respondsToSelector:@selector(setEdgesForExtendedLayout:)]) { contoller.edgesForExtendedLayout = UIRectEdgeNone; contoller.extendedLayoutIncludesOpaqueBars = NO; contoller.automaticallyAdjustsScrollViewInsets = NO; } } #endif }
(二)第二部分(导航栏)
1、uiscrollview适配
本场景是在有uinavigationcontroller和uitabbarcontroller结合的ui框架中。
若整个view的子试图只有uiscrollview的话不需要适配view也能显示正常(我在IOS8、IOS9中测试通过,IOS7因为在xcode7中不支持所以未测试,不敢保证显示正确);若有其他的子视图则需要执行第一部分的方法,且uiscollview的定义要在-(void)viewWillAppear:(BOOL)animated中进行才会显示正常(原因可能是执行适配后view需要重新调整布局,uiscrollview要等到view重新布局完成后才能添加至view试图)。
//uiscrollview是唯一子视图 UIView *tempView = [[UIView alloc] initWithFrame:self.view.bounds]; [self.view addSubview:tempView]; UIScrollView *scroller = [[UIScrollView alloc] initWithFrame:self.view.bounds]; scroller.contentSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height + 150); UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, self.view.bounds.size.height + 110, 200, 40)]; testLabel.text = @"拖到底部就能看见我"; testLabel.textColor = [UIColor blueColor]; scroller.backgroundColor = [UIColor lightGrayColor]; [scroller addSubview:testLabel]; [tempView addSubview:scroller]; //除了uiscrollview,还有其他子视图 //viewDidLoad函数中得代码内容 - (void)viewDidLoad { [super viewDidLoad]; [UIViewController addEdgesForExtendedLayout:self]; self.view.backgroundColor = [UIColor lightGrayColor]; contentView = [[UIView alloc] initWithFrame:self.view.bounds]; [self.view addSubview:contentView]; UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 100)]; topView.backgroundColor = [UIColor orangeColor]; [contentView addSubview:topView]; NSLog(@"%f",self.view.frame.size.height); } //viewWillAppear函数中的代码内容(定义uiscrollview) -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; if (!scroller) { scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, self.view.bounds.size.height - 100)]; scroller.contentSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height + 150); UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, self.view.bounds.size.height + 110, 200, 40)]; testLabel.textColor = [UIColor blueColor]; testLabel.text = @"sdgsdfgdf"; UILabel *testLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 200, 40)]; testLabel1.textColor = [ UIColor blueColor]; testLabel1.text = @"sdgsdfgdf"; [scroller addSubview:testLabel1]; [scroller addSubview:testLabel]; [contentView addSubview:scroller]; } NSLog(@"%f",scroller.frame.size.height); }
下图就是在IOS9下view中只有uiscrollview一个子视图的情况。
2、uitableview适配
场景要有uinavigationbar,否则状态栏会覆盖一部分的内容(已在IOS8、IOS9下测试通过)。
//只有uitableview一个子视图 table = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; table.delegate = self; table.dataSource = self; table.bounces = NO; [self.view addSubview:table]; //若有其他的试图,uitableview的定义须放在-(void)viewWillAppear:(BOOL)animated中才会显示正常: [UIViewController addEdgesForExtendedLayout:self];//这句就是执行第一部分所说的方法(关键) UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 100)]; topView.backgroundColor = [UIColor greenColor]; [self.view addSubview:topView]; UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(10, 20, 100, 30)]; [btn setTintColor:[UIColor blackColor]]; [btn setTitle:@"push" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(pushNext) forControlEvents:UIControlEventTouchUpInside]; [topView addSubview:btn]; NSLog(@"%f",[[[UIDevice currentDevice] systemVersion] floatValue]); NSLog(@"%f",table.frame.size.height); -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; table = [[UITableView alloc] initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, self.view.bounds.size.height - 100) style:UITableViewStylePlain]; //table = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; table.delegate = self; table.dataSource = self; table.bounces = NO; [self.view addSubview:table]; NSLog(@"%f",table.frame.size.height); }
(三)第三部分(重要)
1、uiscrollview、uitableview、uiwebview不需要设置self.edgesForExtendedLayout = UIRectEdgeNone。
2、其他的view,若导航栏透明需要设置self.edgesForExtendedLayout = UIRectEdgeNone等。
3、navigationBar设置成不透明, 要么你整个view 可以上下滚动. 要么设置 self.edgesForExtendedLayout = UIRectEdgeNone;. 要么把导航条设置不透明。
实例:
- (void)viewDidLoad { [super viewDidLoad]; UIView *tempView = [[UIView alloc] initWithFrame:self.view.bounds]; scroller = [[UIScrollView alloc] initWithFrame:self.view.bounds]; [tempView addSubview:scroller]; UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, self.view.bounds.size.width, self.view.bounds.size.height + 200)]; scroller.contentSize = CGSizeMake(contentView.frame.size.width, contentView.frame.size.height); [scroller addSubview:contentView]; UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, contentView.frame.size.height - 40, contentView.frame.size.width, 40)]; testLabel.textColor = [UIColor redColor]; testLabel.text = @"shjdhjsg"; [contentView addSubview:testLabel]; UILabel *testLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, contentView.frame.size.width, 40)]; testLabel1.textColor = [UIColor redColor]; testLabel1.text = @"shjdhjsg"; [contentView addSubview:testLabel1]; NSLog(@"%f",self.view.frame.size.height); [self.view addSubview:tempView]; }
ui布局在contentView。