Some basic global property settings

Posted by Dingbats on Sat, 22 Feb 2020 17:22:19 +0100

"appearance" is really a magic "attribute" in iOS. It is used to set global attributes. For example, it has written the automatic row height setting of 10000 times tableView. (PS. wrote a big bug. iOS 11 two days ago. All cells are the default height. The comment in Xcode9 is the default value, that is, there is no need to write those annoying statements. But I didn't expect that Then, I'll show you how to set the global settings
As you know, the program goes in from the MIA and then a "dead cycle". The code before we create the UI control will be

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

Go inside.
So we can write a code to layout global properties. The usage of appearance is similar to instantiating a class to set properties

- (void)setGobalUIkitAttribute {
    //Set the global properties of UIScrollView
    if (@available(iOS 11.0, *)) {//Solve the problem of top position offset of UIScrollView
        [UIScrollView appearance].contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
        // Set the Scroll enable property of UIViewController
    }
    ///Set the global properties of UITableView
    [UITableView appearance].rowHeight = UITableViewAutomaticDimension;
    [UITableView appearance].estimatedRowHeight = 80;
    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, *)) {//Solve the problem of top position offset of UITableView
        [UITableView appearance].contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
        // Set the Scroll enable property of UIViewController
    }
    //Set the global properties of UICollectionView
    [UICollectionView appearance].backgroundColor = [UIColor whiteColor];
    if (@available(iOS 11.0, *)) {//Solve the problem of top position offset of UICollectionView
        [UICollectionView appearance].contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
        // Set the Scroll enable property of UIViewController
    }
}

It's over here. Do you know how to globally set some annoying attributes that you often need to write

Topics: iOS Attribute