我的iOS学习历程 - 自定义视图
2015-11-12 23:34
495 查看
当你要创建一个例如左边是lable右边是textField的时候怎么样简便一点呢?我们就可以自定义一个视图来将label和textField放在一个view里,让我们以此来举例:
我们先创建一个UILTView的类,把你需要在view上添加的控件定义为属性以方便用// 重写初始化方法 在初始化LTView的同时把label也加上去 - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // 获取动态高度 CGFloat height = frame.size.height; // 获取动态宽度 CGFloat width = frame.size.width; self.label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, width / 5, height)]; self.label.backgroundColor = [UIColor whiteColor]; [self addSubview:self.label]; [_label release]; self.textField = [[UITextField alloc]initWithFrame:CGRectMake(width / 5 + 20, 0, (2 * width / 3) - 20, height)]; self.textField.backgroundColor = [UIColor whiteColor]; self.textField.borderStyle = UITextBorderStyleRoundedRect; [self addSubview:self.textField]; [_textField release]; } return self; }
如果想更方便就可以再创建一个LoginView将你写好的UILTView放进去:
#import "LoginView.h" #define kScreenWidth [UIScreen mainScreen].bounds.size.width #define kScreenHeight [UIScreen mainScreen].bounds.size.height #define kHighDistance 30 // 行间距 #define kButtonDistance 50 // button间距 @implementation LoginView - (void)dealloc { [_userNameLTView release]; [_passwordLTView release]; [_registerButton release]; [_findPasswordButton release]; [_loadingButton release]; [super dealloc]; } - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.userNameLTView = [[LTView alloc]initWithFrame:CGRectMake((kScreenWidth - 300) / 2, 100, 300, 50)]; [self addSubview:self.userNameLTView]; [_userNameLTView release]; self.passwordLTView = [[LTView alloc]initWithFrame:CGRectMake(self.userNameLTView.frame.origin.x, self.userNameLTView.frame.origin.y + self.userNameLTView.frame.size.height + kHighDistance, self.userNameLTView.frame.size.width, self.userNameLTView.frame.size.height)]; [self addSubview:self.passwordLTView]; [self.passwordLTView release]; // 循环button for (int i = 0; i < 3; i ++) { UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)]; button.frame = CGRectMake(50 + (i * 100), 300, 80, 80) ; button.backgroundColor = [UIColor whiteColor]; // 加标签 方便取出button 与属性的button 相对应 button.tag = i + 100; [self addSubview:button]; } // 属性与循环的Button进行关联 self.loadingButton = (UIButton *)[self viewWithTag:100]; self.findPasswordButton = (UIButton *)[self viewWithTag:101]; self.registerButton = (UIButton *)[self viewWithTag:102]; } return self; }
这是创建一个LoginView就是这样子的:
当然手机都会有横屏竖屏的时候,那我们可以这样来调整:(主要是将坐标以及宽高度调整,当手机换方位时,宽高正好互换)
// 重新布局子视图 layoutSubviews // frame发生变化的时候 会触发该方法 - (void)layoutSubviews { // 因为不知道父类的方法做了什么 // 所以在重写的时候 先调用一下父类的方法 // 然后再写咱们自己的 [super layoutSubviews]; // frame发生变化 相当于横屏了 // 这时需要重新布局 // 判断是竖屏还是横屏 // 1.把应用程序取出来 // 2.判断一下当前应用程序 屏幕的朝向 // 取出应用程序 sharedApplication 单例方法的命名规范share+ UIApplication *app = [UIApplication sharedApplication]; // 判断方向 statusBarOrientation if(app.statusBarOrientation == UIInterfaceOrientationPortrait || app.statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown) { self.passwordLTView.frame = CGRectMake(self.userNameLTView.frame.origin.x, self.userNameLTView.frame.origin.y + self.userNameLTView.frame.size.height + kHighDistance, self.userNameLTView.frame.size.width, self.userNameLTView.frame.size.height); }else { self.passwordLTView.frame = CGRectMake(self.userNameLTView.frame.origin.x + self.userNameLTView.frame.size.width + 20, self.userNameLTView.frame.origin.y, self.userNameLTView.frame.size.width, self.userNameLTView.frame.size.height); } }
这就是我们的自定义视图 是不是方便了很多
相关文章推荐
- 我的iOS学习历程 - 自定义视图
- 蓝懿ios 技术内容交流和学习心得 11.12
- iOS程序逆向Mac下常用工具——Reveal、HopperDisassemble、IDA
- iOS如果获取蓝牙Mac地址
- iOS中设置scrollView的偏移量(开始的位置)
- iOS 摇一摇功能的实现
- iOS之Drawing<2>
- ***iOS学习之Table View的简单使用和DEMO示例(共Plain普通+Grouped分组两种)
- iOS中AutoLayer自动布局流程及相关方法
- iOS Autorelease的深入理解
- 欢迎使用CSDN-markdown编辑器
- iOS9 Spotlight使用
- iOS开发-生成随机数
- iOS8+ alert view导致回退键盘闪现的解决
- ios中的绘图机制
- iOS开发零基础教程之Git的一些常用命令
- iOS开发零基础教程之生成git所需的SSH keys
- iOS应用程序生命周期(前后台切换,应用的各种状态)详解
- IOS 多线程分类以及多线程的相关操作
- 编译iOS arm64 armv7 armv7s x86-64的第三方静态库