关于self.view(0,0)点的位置,还有UIScrollView 向下移的问题

前言
朋友问我设置UIScrollView的origin为(0,0)点,但是现实的效果是(0,StatusBar),就是顶部空出了一个状态栏的高度。我就告诉他添加下面代码,能解决问题。

if (@available(iOS 11, *)) {
      self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
 } else {
      self.automaticallyAdjustsScrollViewInsets = NO;
  }

正好就一起扩展一下关于self.View 的(0,0)点的问题

接下来我们看这下面这3个属性

  • extendedLayoutIncludesOpaqueBars
  • edgesForExtendedLayout
  • automaticallyAdjustsScrollViewInsets

1、extendedLayoutIncludesOpaqueBars

1.1如下代码:未设置导航栏的背景
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor redColor];
//默认导航栏是半透明的
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"t"] forBarMetrics:UIBarMetricsDefault];
    NSLog(@"extendedLayoutIncludesOpaqueBars = %d",self.extendedLayoutIncludesOpaqueBars);
    
}

打印结果 : extendedLayoutIncludesOpaqueBars = 0

未设置导航栏背景
1.2 给导航栏设置背景图
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor redColor];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"t"] forBarMetrics:UIBarMetricsDefault];
    NSLog(@"extendedLayoutIncludesOpaqueBars = %d",self.extendedLayoutIncludesOpaqueBars);
    
}

打印结果:extendedLayoutIncludesOpaqueBars = 0

设置了导航栏背景

小结1:extendedLayoutIncludesOpaqueBars默认为NO,在导航栏为透明的时候,self.view 的(0,0)点,在屏幕的左上角,在导航栏不透明的时候,自动移到导航栏的下方,也就是(0,navigationBarHight)

1.3 导航栏未设置背景图,extendedLayoutIncludesOpaqueBars 赋值为YES
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor redColor];
    self.extendedLayoutIncludesOpaqueBars = YES;
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"t"] forBarMetrics:UIBarMetricsDefault];
    NSLog(@"extendedLayoutIncludesOpaqueBars = %d",self.extendedLayoutIncludesOpaqueBars);
    
}

打印结果:extendedLayoutIncludesOpaqueBars = 1

导航栏未设置背景图,extendedLayoutIncludesOpaqueBars 赋值为YES
1.4 导航栏设置背景图,extendedLayoutIncludesOpaqueBars 赋值为YES
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor redColor];
    self.extendedLayoutIncludesOpaqueBars = YES;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"t"] forBarMetrics:UIBarMetricsDefault];
    NSLog(@"extendedLayoutIncludesOpaqueBars = %d",self.extendedLayoutIncludesOpaqueBars);
    
}

打印结果:extendedLayoutIncludesOpaqueBars = 1

导航栏设置背景图,extendedLayoutIncludesOpaqueBars 赋值为YES

小结2:当设置 extendedLayoutIncludesOpaqueBars = YES的时候,不管导航栏是否透明,self.view的(0,0)点都是屏幕的左上角,我们在开发的时候最好设置为YES ,我们就统一计算frame,方便管理
tip:底部的tabbar也是和navigationBar是一样的道理

2、edgesForExtendedLayout ()

2.1edgesForExtendedLayout 设置为 UIRectEdgeAll(这也是默认值)
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor redColor];
    self.edgesForExtendedLayout = UIRectEdgeAll;
}
UIRectEdgeAll
2.2 edgesForExtendedLayout 设置为 UIRectEdgeTop
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor redColor];
    self.edgesForExtendedLayout = UIRectEdgeTop;
}
UIRectEdgeTop
2.3 edgesForExtendedLayout 设置为 UIRectEdgeBottom
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor redColor];
    self.edgesForExtendedLayout = UIRectEdgeBottom;
}
UIRectEdgeBottom
2.4 edgesForExtendedLayout 设置为 UIRectEdgeNone
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor redColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
}
UIRectEdgeNone

总结:通过设置edgesForExtendedLayout此属性,你可以指定view的边(上、下、左、右)延伸到整个屏幕。默认为UIRectEdgeAll

3、automaticallyAdjustsScrollViewInsets

automaticallyAdjustsScrollViewInsets(默认YES)这个属性在iOS11 已经被放弃使用了,使用UIScrollViewcontentInsetAdjustmentBehavior

automaticallyAdjustsScrollViewInsets

所以我们在使用的时候要兼容老版本,代码如下:

if (@available(iOS 11, *)) {
      self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  } else {
      self.automaticallyAdjustsScrollViewInsets = NO;
  }

这个我就不写的Demo了

总结:开发的时候我们就设置 ,让(0,0)点在屏幕的左上角,方便计算frame
self.extendedLayoutIncludesOpaqueBars = YES
if (@available(iOS 11, *)) {
self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
self.automaticallyAdjustsScrollViewInsets = NO;
}

在Objective-C中,如果你有一个`UIView`类型的变量`parentView`,并想刷新它,通常你可以调用视图的一些更新或布局相关的函数。例如,如果你想让视图重新布局,你可以使用`layoutIfNeeded`或者`layoutSubviews`方法。如果是改变内容,比如重新绘制,那么可以根据视图类型来调用对应的刷新方法,比如对于UIImageView可能是`setNeedsDisplay`,对于UILabel则是`setNeedsLayout`。 下面是针对`parentView`的一些常见操作示例: ```objc if ([parentView respondsToSelector:@selector(setNeedsDisplay)]) { [parentView setNeedsDisplay]; } else if ([parentView respondsToSelector:@selector(setNeedsLayout)]) { [parentView setNeedsLayout]; [parentView layoutIfNeeded]; // 如果视图需要重新计算大小和位置 } else if ([parentView respondsToSelector:@selector(reloadData)]) { // 这里假设parentView有一个reloadData方法用于刷新数据 [parentView reloadData]; } // 如果parentView是一个响应布局的容器视图(如UIScrollView) [parentView setContentOffset:CGPointZero animated:YES]; // 刷新滚动位置 [parentView scrollRectToVisible:CGRectMake(0, 0, parentView.bounds.size.width, parentView.bounds.size.height) animated:YES]; // 显示整个视图 // 如果需要更新frame,可以直接设置 [parentView setFrame:self.view.bounds]; // 将视图的frame设置为其父视图的边界 ``` 请注意,上述代码取决于`parentView`的具体类型和需求。在实际应用中,你应该查阅相应的文档以获取正确的刷新方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值