关于UIButton如何调整内部子控件
来源:互联网 发布:二手车交易平台源码 编辑:程序博客网 时间:2024/05/02 01:43
layoutSubviews在以下情况下会被调用:
1、init初始化不会触发layoutSubviews2、addSubview会触发layoutSubviews3、设置view的Frame会触发layoutSubviews,当然前提是frame的值设置前后发生了变化4、滚动一个UIScrollView会触发layoutSubviews5、旋转Screen会触发父UIView上的layoutSubviews事件6、改变一个UIView大小的时候也会触发父UIView上的layoutSubviews事件
自定义按钮的实现文件,继承于UIButton#import "WkButton.h"@implementation WkButton-(instancetype)initWithFrame:(CGRect)frame//只要通过代码方式创建对象,就会调用initwithframe方法{ if (self = [super initWithFrame:frame]) { self.titleLabel.textAlignment = NSTextAlignmentCenter; self.imageView.contentMode = UIViewContentModeScaleAspectFill; } return self;}#pragma make - 方式一:重写两个方法//重写2个方法// -(CGRect)titleRectForContentRect:(CGRect)contentRect// {// return CGRectMake(0, 0, 100, 70);// }// -(CGRect)imageRectForContentRect:(CGRect)contentRect// {// return CGRectMake(100, 0, 70, 70);// }#pragma mark - 方式二:layoutSubviews-(void)layoutSubviews{ [super layoutSubviews]; //设置子控件的位置 self.titleLabel.frame = CGRectMake(0, 0, 100, 70); self.imageView.frame = CGRectMake(100, 0, 70, 70);}@end//// ViewController.m// Wk-day1//// Created by Mac on 16/8/22.// Copyright © 2016年 Mac. All rights reserved.//#import "ViewController.h"#import "WkButton.h"@interface ViewController ()@end控制器的实现文件@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; UIButton *button = [WkButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(100, 100, 170, 70);//此处触发layoutSubviews button.backgroundColor = [UIColor purpleColor]; [button setTitle:@"普通按钮" forState:UIControlStateNormal]; [button setImage:[UIImage imageNamed:@"miniplayer_btn_playlist_normal"] forState:UIControlStateNormal]; button.imageView.backgroundColor = [UIColor yellowColor]; button.titleLabel.backgroundColor = [UIColor blueColor]; [self.view addSubview:button];}@end
0 0
- 关于UIButton如何调整内部子控件
- iOS 关于scrollView内部子控件的自动布局
- UIButton,送给多数开发者的释疑文章,关于如何正确调整imageView及titleLabel的位置
- 如何调整UIButton里面的文字位置
- CocoStudio UIButton setPressedActionEnabled 子控件缩放解决方案
- ios - 调整button内部控件位置
- UIButton如何正确调整imageView及titleLabel的位置
- UIButton如何正确调整imageView及titleLabel的位置
- UIButton如何正确调整imageView及titleLabel的位置
- iOS UIButton如何正确调整imageView及titleLabel的位置
- JFrame自动调整大小适应子控件
- DataGridTemplateColumn 如何获取内部控件
- 获取 ViewPager 内部子控件的方法
- UIButton是UIImageView的子控件无法与用户交互
- UIButton 控件
- UIButton控件
- UIButton调整字体大小
- UIButton背景图片调整大小
- leetcode-java-357. Count Numbers with Unique Digits
- windows对象的属性
- 两年多工作心得和体会
- 概率图模型(03): 模板模型(动态贝叶斯, 隐马尔可夫和Plate模型)
- Codeforces Round #368 (Div. 2) [E] Garlands
- 关于UIButton如何调整内部子控件
- 策略模式
- tomcat安装常见问题
- 新的开始~为了最爱的那个她努力
- sockaddr_in结构体简介
- Myeclipse安装 配置Maven
- L2-012. 关于堆的判断-PAT甲级真题(堆的建立,向上调整)
- Android自定义View之(PullDoorView)
- [gcc编程] socket编程——sockaddr_in结构体操作