用Xib自定义一个View
2016-07-20 14:23
357 查看
1. 新建一个继承于View的类,再建一个有view的xib文件,选中这个xib的file’s Owner,修改Custom Class .
2. 把size选成FreeForm,这样你就可以随意的调整View的大小了。
3.在Xib上布局好
4. 下面就是如何使用这个Xib。有两种方法。
使用代码方法初始化。
*先把xib的view设置成一个属性。
*然后在自定义的view中,写入代码
使用方法:
2.直接Xib初始化。
这样就可以正常的使用Xib自定义的View了,(配色真难看。)
2. 把size选成FreeForm,这样你就可以随意的调整View的大小了。
3.在Xib上布局好
4. 下面就是如何使用这个Xib。有两种方法。
使用代码方法初始化。
*先把xib的view设置成一个属性。
*然后在自定义的view中,写入代码
- (instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { [self awakeFromNib]; } return self; } - (void)awakeFromNib{ self.contentView = [[[NSBundle mainBundle]loadNibNamed:@"WeiShopHeaderView" owner:self options:nil]lastObject]; self.contentView.frame = self.bounds; [self addSubview:self.contentView]; }
使用方法:
//需要设置frame。 WeiShopHeaderView *head = [[WeiShopHeaderView alloc]initWithFrame:CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, 290)]; head.frame = CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, 290); [self.view addSubview:head];
2.直接Xib初始化。
WeiShopHeaderView *head = [[[NSBundle mainBundle]loadNibNamed:@"WeiShopHeaderView" owner:self options:nil]lastObject]; [self.view addSubview:head]; //vfl 自动布局 head.translatesAutoresizingMaskIntoConstraints = NO; NSDictionary *viewsDict = NSDictionaryOfVariableBindings(head); [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[head]|" options:0 metrics:nil views:viewsDict]]; [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-100-[head(290)]" options:0 metrics:nil views:viewsDict]]; head.backgroundColor = [UIColor redColor];
这样就可以正常的使用Xib自定义的View了,(配色真难看。)
相关文章推荐
- 配置View桌面时找不到域的解决方法
- 完全克隆的虚拟桌面部署问题
- jquery mobile 实现自定义confirm确认框效果的简单实例
- 实例讲解JavaScript的Backbone.js框架中的View视图
- C#实现自定义双击事件
- WinForm实现自定义右下角提示效果的方法
- Android中View自定义组合控件的基本编写方法
- Android 自定义View步骤
- 一款超酷的Android自定义加载控件
- Android自定义View仿QQ健康界面
- Android重写View实现全新的控件
- 解读ASP.NET 5 & MVC6系列教程(16):自定义View视图文件查找逻辑
- MFC自定义消息的实现方法
- C#实现ProperTyGrid自定义属性的方法
- php自定义错误处理用法实例
- ThinkPHP中自定义目录结构的设置方法
- C#自定义事件监听实现方法
- C#自定义事件及用法实例
- C#自定义签名章实现方法
- C#中使用IFormattable实现自定义格式化字符串输出示例