美文网首页
extendedLayoutIncludesOpaqueBars

extendedLayoutIncludesOpaqueBars

作者: MR_詹 | 来源:发表于2020-03-28 23:04 被阅读0次

文章更新请移步到github.page浏览

#import "ViewController.h"
#define kCellId @"CellId"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong, readwrite) UITableView         *tableView;
@property (nonatomic, strong, readwrite) NSMutableArray      *dataArray;
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    /*
        知识点一:
        导航栏或者是导航栏上的控件设置透明度是没有效果的
        如果要达到透明的效果可以通过颜色设置,比如navigationBar
        的背景图片BackgroundImage,或者导航栏上的标题
     */
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:UITableViewCell.class forCellReuseIdentifier:kCellId];
    [self.view addSubview:self.tableView];

    self.dataArray = [NSMutableArray array];
    for (int i=0; i<30; i++) {
        NSString *string = [NSString stringWithFormat:@"第%d行",i];
        [self.dataArray addObject:string];
    }
    
//    UIView *blueView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 128)];
//    blueView.backgroundColor = [UIColor blueColor];
//    [self.view addSubview:blueView];
    
    /*
       知识点二:
       以下的方法是设置UIScrollView及其子类,在滚动视图内容的时候自动偏移,不会被UINavigationBar和UITabBar遮盖
     */
    // @available(iOS 11,*)
    // 第一个参数是指iOS11以上的系统,
    // 第二个是指任何平台
    if (@available(iOS 11,*)) {
        // iOS11 引入的“安全区域”的概念,废弃了iOS7之后出现的 topLayoutGuide/bottomLayoutGuide
        // 常用的两个值
        // UIScrollViewContentInsetAdjustmentAutomatic (默认值,设置ScrollView的显示区域范围在safeArea的范围内,不被stateBar、TabBar、NavigationBar遮盖)
        // UIScrollViewContentInsetAdjustmentNever (设置ScrollView全屏展示,布局起点是屏幕的最左边)
        // 其原理与iOS11的不一样,scrollView.contentInset的值不会变,而是设置了
        // scrollView.ajustedContentInset的值(提供给用户的是只读属性)
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
        // 另外用户还可以增加“安全区域”的范围
        // 备注:设置的值是在原来得基础上设置的,不能减少
        self.additionalSafeAreaInsets = UIEdgeInsetsMake(200, 0, 0, 0);
        // 备注:contentInsett和additionalSafeAreaInsets 两个同时设置,作用不会重叠,是以设置最大的为准
//        self.tableView.contentInset = UIEdgeInsetsMake(50, 0, 0, 0);
        
    }
    else{
        // 在iOS11.0系统之后,被弃用了
        // 其原理是设置scrollView.contentInset,可以在viewDidAppear方法打印其值
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    /**
     extendedLayoutIncludesOpaqueBars 默认是NO(备注:对UIScrollView及其子类不起作用)
     1、当为YES时,view的坐标原点是屏幕的最左上角开始的,与navigationBar的透明度Translucent无关
     2、当为NO时
        a> navigationBar.translucent = YES,view的布局起点是从屏幕的最左上角开始
        b> navigationBar.translucent = NO,view的布局起点是从导航栏左下角开始
     */
    self.extendedLayoutIncludesOpaqueBars = NO;
    
    
    /**
     常用两个值:
     UIRectEdgeNone (对是有的UIView都有效,ScrollView及其子类都是从导航栏底部布局,优先级比较高,不受其他属性影响)
     UIRectEdgeAll  (默认值)全屏布局,但是会受多个属性影响,比如extendedLayoutIncludesOpaqueBars、navigationBar.translucent、scrllView的安全区域设置等
    */
    self.edgesForExtendedLayout = UIRectEdgeAll;
    
    
    /*
     不会对ScrollView的布局产生影响,只会对其他的UIView产生影响
     */
    [self.navigationController.navigationBar setTranslucent:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellId forIndexPath:indexPath];
    cell.textLabel.text = [self.dataArray objectAtIndex:indexPath.row];
    cell.contentView.backgroundColor = [UIColor redColor];
    return cell;
}


- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSLog(@"navigationBar.translucent %d",self.navigationController.navigationBar.translucent);
    NSLog(@"self.tableView.contentInset %@",NSStringFromUIEdgeInsets(self.tableView.contentInset));
    NSLog(@"self.tableView.adjustedContentInset %@",NSStringFromUIEdgeInsets(self.tableView.adjustedContentInset));
}

相关文章

网友评论

      本文标题:extendedLayoutIncludesOpaqueBars

      本文链接:https://www.haomeiwen.com/subject/vdpeuhtx.html