UIView作为iOS开发里基本控件,是我们第一个需要学习的。下面我来为大家介绍一下UIView的一些常用属性和它们的用法。
这里附上UI控件演示的源码地址:https://github.com/LOLR2017/UIKitDemo。源码持续更新中...
因为部分代码是从Xcode直接拷贝出来的样式有误请谅解。
下面是UIView的基本用法。
//初始化一个view
self.mineView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
//设置view的位置和大小
self.mineView.frame = CGRectMake(100, 100, 100, 100);
//设置view的背景颜色
self.mineView.backgroundColor = [UIColor redColor];
//设置view的边框颜色
self.mineView.layer.borderColor = [UIColor greenColor].CGColor;
//设置view的边框宽度
self.mineView.layer.borderWidth = 3;
//设置view的圆角
self.mineView.layer.cornerRadius = 10;
self.mineView.layer.maskedCorners = YES;
/*
因为阴影超出了view的图层,所以圆角和阴影不能同时设置,如何达到圆角和阴影的效果后面会讲到。
*/
//设置阴影
self.mineView.layer.shadowColor = [UIColor blackColor].CGColor;
self.mineView.layer.shadowRadius = 10;
self.mineView.layer.shadowOffset = CGSizeMake(0, 10);
self.mineView.layer.shadowOpacity = 0.5;
//将view添加到视图上
[self.view addSubview:self.mineView];
这里附上UI控件演示的源码地址:https://github.com/LOLR2017/UIKitDemo。源码持续更新中...
写在结尾:本文初衷是提供给一些新手或者需要某些资料查询者,代码或者思想有不足之处请大家谅解,希望大家多多原谅。最后希望大家可以共同进步,快乐工作。
下一篇文章: 《UILabel的基本使用》