“appearance"在iOS中真是一个神奇的"属性”.他是用来做全局属性设置的.就比如写了一万遍的tableView的自动计算行高的设置.(PS.前两天写了个大BUG.iOS11一下所有的Cell都是默认高度,Xcode9中注释是默认值,也就是不需要写那些烦人的语句了.但是没想到…还是要写).那么,下面我来介绍一下如何进行全局的设置.
大家知道.程序是从mian里头进去然后就一个"死循环".我们创建UI控件之前的代码会在
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
里头走.
那么我们可以写一个代码去布局全局的属性.appearance的用法其实和实例化一个类去设置属性差不多.
- (void)setGobalUIkitAttribute {
//设置UIScrollView的全局属性
if (@available(iOS 11.0, *)) {//解决UIScrollView的顶部位置偏移的问题
[UIScrollView appearance].contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
// Fallback on earlier versions
// 设置UIViewController的Scroll enable属性.
}
///设置UITableView的全局属性.
[UITableView appearance].rowHeight = UITableViewAutomaticDimension;
/// 设置自动计算行高.iOS11以上系统默认设置了.但是iOS11以下不设置的话自动计算行高无法撑开.
[UITableView appearance].estimatedRowHeight = 80;
/// 每一组的组头组委自动计算高度设置.如果不设置可能会出现闪动的情况
[UITableView appearance].estimatedSectionFooterHeight = 0;
[UITableView appearance].estimatedSectionHeaderHeight = 0;
/// 组头组尾设置.如果不设置的话会在iOS10.1版本上出现BUG.
CGRect zeroRect = CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, CGFLOAT_MIN);
UIView *header = [[UIView alloc] initWithFrame:zeroRect];
[UITableView appearance].tableHeaderView = header;
[UITableView appearance].tableFooterView = header;
[UITableView appearance].tableHeaderView.frame = zeroRect;
[UITableView appearance].tableFooterView.frame = zeroRect;
if (@available(iOS 11.0, *)) {//解决UITableView的顶部位置偏移的问题
[UITableView appearance].contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
// Fallback on earlier versions
// 设置UIViewController的Scroll enable属性.
}
//设置UICollectionView的全局属性
[UICollectionView appearance].backgroundColor = [UIColor whiteColor];
if (@available(iOS 11.0, *)) {//解决UICollectionView的顶部位置偏移的问题
[UICollectionView appearance].contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
// Fallback on earlier versions
// 设置UIViewController的Scroll enable属性.
}
}
到这里结束.大家是不是知道如何全局设置一些经常要写的恼人的属性了.