一些基础的全局属性设置

“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属性.
    }
}

到这里结束.大家是不是知道如何全局设置一些经常要写的恼人的属性了.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值