layoutSubviews总结

原文地址:点击打开链接

今天跟旺才兄学习了一下UIView的setNeedsDisplay和setNeedsLayout方法。首先两个方法都是异步执行的。而setNeedsDisplay会调用自动调用drawRect方法,这样可以拿到UIGraphicsGetCurrentContext,就可以画画了。而setNeedsLayout会默认调用layoutSubViews,就可以处理子视图中的一些数据。

上所诉,setNeedsDisplay方便绘图,而layoutSubViews方便出来数据。

ipad横竖屏切换解决方案
2011年08月01日 星期一 10:09
由于ipad的横竖屏不同,所以好的应用,横竖屏的页面布局也不一样。那么就需要横竖屏的整体解决方案。先看一个横竖屏布局不一样的界面。

上面两张图是来自同一个界面的横竖版的截屏。可以看出,横竖版显示的内容相同,但是界面布局不同。要实现上述布局,主要是运用UIView中 layoutSubviews方法。当UIView设置为自动适配屏幕时,当用户旋转设备的时候,会调用layoutSubviews方法,我们只需重写 这个方法,然后判断用户屏幕的方向。在调整每个空间的位置即可。

下面是实现上述界面的最简单的原型:

首先分析可以知道左面是图片,右面是一个图片加文字的视图。下面就实现一个左面视图右面是一个图加一段字的事例。
事例的截图如下:

其中右面的文字和绿色部分是用一个子视图封装的。
整个布局是我在主视图中添加了一个ContentView视图,在ContentView视图中添加了一个ArticleView视图。
其中ArticleView和ContentView的xib文件都打开了


[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 在ContentView中重写layoutSubviews方法,然后根据stausbar的方向判断当前视图的横竖屏。具体代码:  
  2. -(void)layoutSubviews{   
  3. [super layoutSubviews];   
  4. UIDeviceOrientation interfaceOrientation=[[UIApplication sharedApplication] statusBarOrientation];   
  5. if (interfaceOrientation == UIDeviceOrientationPortrait || interfaceOrientation == UIDeviceOrientationPortraitUpsideDown) {   
  6. //翻转为竖屏时   
  7. [self setVerticalFrame];   
  8. }else if (interfaceOrientation==UIDeviceOrientationLandscapeLeft || interfaceOrientation == UIDeviceOrientationLandscapeRight) {   
  9. //翻转为横屏时   
  10. [self setHorizontalFrame];   
  11. }   
  12. }  
  13.   
  14. -(void)setVerticalFrame   
  15. {   
  16. NSLog(@"竖屏");   
  17. [titleLable setFrame:CGRectMake(283023983)];   
  18. [leftView setFrame:CGRectMake(38102384272)];   
  19. [rightView setFrame:CGRectMake(450102282198)];   
  20. }  
  21.   
  22. -(void)setHorizontalFrame   
  23. {   
  24. NSLog(@"横屏");   
  25. [titleLable setFrame:CGRectMake(183023983)];   
  26. [leftView setFrame:CGRectMake(168122384272)];   
  27. [rightView setFrame:CGRectMake(650122282198)];   
  28. }  
  29.   
  30. 在具体的横竖屏方法中,从新设置各个组件的坐标即可。  
  31.   
  32. 接下来在ContentView中添加ArticleView视图。  
  33. -(id)initWithCoder:(NSCoder *)aDecoder   
  34. {   
  35. if ((self = [super initWithCoder:aDecoder])) {  
  36.   
  37. NSArray *arrayContentView =[[NSBundle mainBundle] loadNibNamed:@"ArticleView" owner:self options:nil];   
  38. rightView=[arrayContentView objectAtIndex:0];   
  39. [self addSubview:rightView];   
  40. }   
  41. return self;   
  42. }  
  43.   
  44. 由于我用的是xib,所以初始化方法为initWithCoder,在这个中添加新的视图。  
  45.   
  46. 同样在ArticleView中设置横竖屏相应空间的坐标即可。  
  47. -(void)layoutSubviews{   
  48. [super layoutSubviews];   
  49. UIDeviceOrientation interfaceOrientation=[[UIApplication sharedApplication] statusBarOrientation];   
  50. CGRect rect=self.frame;   
  51. rect.size.width=282;   
  52. rect.size.height=198;   
  53. [self setFrame:rect];   
  54. if (interfaceOrientation == UIDeviceOrientationPortrait || interfaceOrientation == UIDeviceOrientationPortraitUpsideDown) {   
  55. //翻转为竖屏时   
  56. [self setVerticalFrame];   
  57. }else if (interfaceOrientation==UIDeviceOrientationLandscapeLeft || interfaceOrientation == UIDeviceOrientationLandscapeRight) {   
  58. //翻转为横屏时   
  59. [self setHorizontalFrame];   
  60. }   
  61. }  
  62.   
  63. -(void)setVerticalFrame   
  64. {   
  65. NSLog(@"竖屏");   
  66. [contentView setFrame:CGRectMake(126250125)];   
  67. [textLable setFrame:CGRectMake(5013918239)];   
  68. }  
  69.   
  70. -(void)setHorizontalFrame   
  71. {   
  72. NSLog(@"横屏");   
  73. [contentView setFrame:CGRectMake(126106158)];   
  74. [textLable setFrame:CGRectMake(1351114739)];   
  75. }  

@layoutSubviews何时调用的问题

layoutSubviews何时调用的问题,这个方法是当你需要在调整subview的大小的时候需要重写(我这个翻译不严谨,以下是原文:You should override this method only if the autoresizing behaviors of the subviews do not offer the behavior you want.),但有时候经常指望它被调用的时候没被调用,不希望它被调用的时候被调用了,搞的很上火。根据国外社区一个人帖子,做了总结性翻译。

layoutSubviews在以下情况下会被调用:

1、init初始化不会触发layoutSubviews
2、addSubview会触发layoutSubviews
3、设置view的Frame会触发layoutSubviews,当然前提是frame的值设置前后发生了变化
4、滚动一个UIScrollView会触发layoutSubviews
5、旋转Screen会触发父UIView上的layoutSubviews事件
6、改变一个UIView大小的时候也会触发父UIView上的layoutSubviews事件

  • 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、付费专栏及课程。

余额充值