layoutSubviews在什么情况下调用

  • 1.在以下情况都会调用
    • 1.直接调用[self setNeedsLayout];(这个在上面苹果官方文档里有说明)
    • 2.addSubview的时候。
    • 3.当view的size发生改变的时候。
    • 4.滑动UIScrollView的时候。
    • 5.旋转Screen会触发父UIView上的layoutSubviews事件。
      注意:当view的size的值为0的时候,addSubview也不会调用layoutSubviews。当要给这个view添加子控件的时候不管他的size有没有值都会调用
  • 2.先来看一下UIView的layoutSubviews在什么情况下会调用
    • 2.1初始化,当size不为0的时候调用一次,当size为0的时候不会调用(在任何方法里面都不会调用)
      SubView*view= [[SubView alloc]initWithFrame:CGRectMake(0,0,100,100)];
      view.backgroundColor=[UIColor redColor];
      [self.view addSubview:view];
      self.sbView= view;
    • 2.2当size改变的时候调用一次(size每次都不同,如果size相同就不会调用)
    • 2.3当添加子控件的时候,不管子控件有没有尺寸都会调用例如:
      // 添加子控件的时候都会调用
      CustomBtn *button = [[CustomBtn alloc] init];
      [self.sbView addSubview:button];
  • 3.看UIButton什么时候调用
    • 3.1这种情况下button的layoutSubviews调用一次,因为当控制器的view显示的时候会调用控制器view的layoutSubviews(UIView除外,比较特殊)
    • - (void)viewDidLoad{
       [super viewDidLoad];
      CustomBtn*button = [[CustomBtn alloc]init];
      [self.view addSubview:button];
      }
    • 如果放在 viewDidAppear button的layoutSubviews就不会调用
      - (void)viewDidAppear:(BOOL)animated{
      [super viewDidAppear:animated];
      // 因为view这时候已经显示了,父控件就不会布局了,所以代码写在这里最好,受干扰的因素最小
      CustomBtn*button = [[CustomBtn alloc]init];
      [self.view addSubview:button];
      }
    • 3.2设置title会调用一次layoutSubviews
      CustomBtn*button = [[CustomBtn alloc] init];
      [button setTitle:@"xxoo" forState:UIControlStateNormal]; 
      [self.view addSubview:button];
    • 3.2设置image的时候会调用一次layoutSubviews,注意:如果图片的名字在工程里面没有,就不会调用,别想骗过苹果
      CustomBtn *button = [[CustomBtn alloc] init];
      [button setImage:[UIImage imageNamed:@"xoxo"] forState:UIControlStateNormal];
      [self.view addSubview:button];
  • 4.UIScrollView
    • 4.1这种情况也会调用(比较特殊)
      // scrollView比较特殊
      CustomScrollView *scrollView= [[CustomScrollView alloc]init];
      [self.view addSubview:scrollView];
    • 4.2这种情况会调用一次layoutSubviews(代码放在viewDidAppear,防止干扰)
      // 因为scrollView有size,所以会调用
      CustomScrollView *scrollView= [[CustomScrollView  alloc]initWithFrame:CGRectMake(100,100,300,200)];
      [self.view addSubview:scrollView];
      scrollView.backgroundColor= [UIColor yellowColor];
      scrollView.contentSize= CGSizeMake(0,500);
    • 4.3当UIScrollView滑动的时候会掉多次layoutSubviews
    • 文章转自:http://www.jianshu.com/p/d1ce00f08b7f
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 iOS 中,可以使用 Objective-C 自定义 TabBar。下面是一个简单的步骤: 1. 创建一个继承自 UITabBarController 的类,例如 MyTabBarController。 2. 在 MyTabBarController.m 中,实现 viewDidLoad 方法,在其中创建自定义 TabBar。 ```objc - (void)viewDidLoad { [super viewDidLoad]; // 创建自定义 TabBar MyTabBar *myTabBar = [[MyTabBar alloc] init]; myTabBar.delegate = self; [self setValue:myTabBar forKey:@"tabBar"]; } ``` 3. 在 MyTabBarController.m 中,实现 UITabBarDelegate 协议中的方法,例如 tabBar:didSelectItem:,处理 TabBar 点击事件。 ```objc - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item { // 处理 TabBar 点击事件 } ``` 4. 创建一个继承自 UITabBar 的类,例如 MyTabBar。 5. 在 MyTabBar.m 中,实现 layoutSubviews 方法,在其中布局 TabBar。 ```objc - (void)layoutSubviews { [super layoutSubviews]; // 布局 TabBar } ``` 6. 在 MyTabBar.m 中,实现 drawRect: 方法,在其中绘制 TabBar。 ```objc - (void)drawRect:(CGRect)rect { // 绘制 TabBar } ``` 7. 在 MyTabBar.m 中,实现 touchUpInside 方法,在其中处理 TabBar 点击事件。 ```objc - (void)touchUpInside:(UIButton *)button { // 处理 TabBar 点击事件 } ``` 8. 在 MyTabBar.m 中,添加 TabBar 上的按钮,例如: ```objc - (void)layoutSubviews { [super layoutSubviews]; // 添加 TabBar 上的按钮 UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom]; button1.frame = CGRectMake(0, 0, self.frame.size.width / 4, self.frame.size.height); [button1 addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button1]; UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom]; button2.frame = CGRectMake(self.frame.size.width / 4, 0, self.frame.size.width / 4, self.frame.size.height); [button2 addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button2]; UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom]; button3.frame = CGRectMake(self.frame.size.width / 2, 0, self.frame.size.width / 4, self.frame.size.height); [button3 addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button3]; UIButton *button4 = [UIButton buttonWithType:UIButtonTypeCustom]; button4.frame = CGRectMake(self.frame.size.width * 3 / 4, 0, self.frame.size.width / 4, self.frame.size.height); [button4 addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button4]; } ``` 9. 在 MyTabBar.m 中,重写 setItems:animated: 方法,在其中设置 TabBar 上按钮的图标和标题。 ```objc - (void)setItems:(NSArray<UITabBarItem *> *)items animated:(BOOL)animated { [super setItems:items animated:animated]; // 设置 TabBar 上按钮的图标和标题 for (int i = 0; i < items.count; i++) { UITabBarItem *item = items[i]; UIButton *button = self.subviews[i + 1]; [button setImage:item.image forState:UIControlStateNormal]; [button setTitle:item.title forState:UIControlStateNormal]; [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; } } ``` 以上就是使用 Objective-C 自定义 TabBar 的简单步骤。当然,还可以根据需求进行更详细的定制。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值