更新了iOS11之后,系统APP大量使用了新的UI风格,那就是iOS11的新特性-Large Title和新的SearchController
Large Title & Table View
设置Large Title
- APP全局使用LargeTitle
//设置Large Title
if (@available(iOS 11.0, *)) {
[[UINavigationBar appearance]setPrefersLargeTitles:true];
} else {
// Fallback on earlier versions
}
- 单个ViewController使用Large Title
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//设置Large Title偏好设置为True
if (@available(iOS 11.0, *)) {
[self.navigationController.navigationBar setPrefersLargeTitles:true];
} else {
// Fallback on earlier versions
}
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
//设置Large Title偏好设置为false
if (@available(iOS 11.0, *)) {
[self.navigationController.navigationBar setPrefersLargeTitles:false];
} else {
// Fallback on earlier versions
}
}
使用上述代码设置后,即可开启Large Title的显示
//设置Title
[self setTitle:@"title"];
添加Table View
1513159623621.jpg在Xcode 9 中,XIB
和StoryBoard
默认会添加Safe Area(安全区)
,而添加在Controller的view上控件的约束也不再以superview为准,而是以Safe Area
为准
Safe Area
是在iOS 9 中添加的特性,如果你不需要使用Safe Area,或者需要在旧版本的APP中添加Safe Area
,可以在XIB或StoryBoard的右侧边栏中Show The File inspector
标签的Interface builder Document
栏下对Use Safe Area Layout Guides
进行勾选,以添加或删除Safe Area
添加Refresh Control
Refresh Control 是系统的下拉刷新控件,配合UITableView使用,以实现系统的下拉属性效果
self.refreshControl = [[UIRefreshControl alloc]init];
[self.refreshControl addTarget:self action:@selector(refreshControlAction) forControlEvents:UIControlEventValueChanged];
if (@available(iOS 11.0, *)) {
[self.tableView setRefreshControl:self.refreshControl];
} else {
// Fallback on earlier versions
}
刷新完成调用- (void)endRefreshing NS_AVAILABLE_IOS(6_0);
结束刷新
- (void)refreshControlAction{
NSLog(@"refreshcontrolAction");
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.refreshControl endRefreshing];
});
}
Search Controller
添加搜索栏
if (@available(iOS 11.0, *)) {
UISearchController *searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
self.navigationItem.searchController = searchController;
} else {
// Fallback on earlier versions
}
关于Search Controller的使用再次不在累赘,可参考官方Sample Code Table Search with UISearchController
再说几句
- 由于Large Title及一些API仅在iOS 11提供,所以在使用前均添加了判断,以防止低版本系统调用高版本API造成应用崩溃
if (@available(iOS 10.0, *)) {
// Code...
} else {
// Fallback on earlier versions
}
- 在实际开发中,在ViewController上添加TableView实现效果不够理想,在滑动过程中有明显卡顿,而直接使用TableViewController实现效果最为理想,滑动无卡顿现象
网友评论