道虽迩,不行不至;事虽小,不为不成。

相关阅读

1.iOS-UI控件精讲之UIView(本文)

2.iOS-UI控件精讲之UILabel

...待续

UIView是所有UI控件的基类,在布局的时候通常会使用UIView作为容器对控件进行分组。

1.首先看一下所有的UI控件的继承关系

UIView中的属性和方法定义了所有的UI控件的公共行为,UIView中所有的public属性,你在它的所有的子控件中都可以使用的。

2.UIView中常见的属性和方法

2.1几何相关

//这几个属性都支持隐式动画的
@property(nonatomic) CGRect frame;//view的相对于父控件的位置(x,y)和大小(width,height)
@property(nonatomic) CGRect bounds; //view的相对于自身的位置(x,y)和大小(width,height) (x,y)一般为(0,0)
@property(nonatomic) CGPoint center;//view的中心点相对于父控件的位置
@property(nonatomic) CGAffineTransform transform; //view的形变属性

//添加一个view并设置红色背景色
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor redColor];
view.frame = CGRectMake(150, 300, 100, 100);
//设置旋转45度的形变属性
view.transform = CGAffineTransformRotate(view.transform, M_PI_4);
[self.view addSubview:view];



2.2视图从属关系

@property(nullable, nonatomic,readonly) UIView       *superview;//所属父类
@property(nonatomic,readonly,copy) NSArray<__kindof UIView *>*subviews;//所有的子类
@property(nullable, nonatomic,readonly) UIWindow *window;//所属的window - (void)removeFromSuperview;//从父类中移除
- (void)addSubview:(UIView *)view;//添加子类
- (nullable __kindof UIView *)viewWithTag:(NSInteger)tag;//通过tag搜索子view

2.3其他

@property(nonatomic)                                 NSInteger tag;//设置tag,主要是为了跟别的view进行区分
@property(nonatomic,getter=isUserInteractionEnabled) BOOL userInteractionEnabled;//是否允许交互 @property(nonatomic,readonly,strong) CALayer *layer;//view的图层,view显示的内容layer属性决定的。

下面看这个layer的例子

UIView *view = [[UIView alloc] init];
//这里我已经设置了backgroundColor为redColor
view.backgroundColor = [UIColor redColor];
//在这里我又设置了layer的backgroundColor为brownColor
view.layer.backgroundColor = [UIColor brownColor].CGColor; view.frame = CGRectMake(150, 300, 100, 100);
[self.view addSubview:view];

关于layer的应用还有两个比较常见的

//1.圆角
view.layer.cornerRadius = 10;//后面的这个值越大就越圆,等宽高的view的宽度的一半就是一个圆形

设置边框
view.layer.cornerRadius = view.bounds.size.width / 2;
//设置边框的宽度
view.layer.borderWidth = 3;
//设置边框的颜色
view.layer.borderColor = [UIColor blueColor].CGColor;

clipsToBounds属性

@property(nonatomic)                 BOOL              clipsToBounds;//超出边框是否剪切

我们先看看超出边框的情况

//红色view
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor redColor];
view.frame = CGRectMake(150, 300, 100, 100);
[self.view addSubview:view];
//往红色的view上面添加棕色view
UIView *v = [[UIView alloc] init];
v.backgroundColor = [UIColor brownColor];
v.frame = CGRectMake(50, 50, 100, 100);
[view addSubview:v];
//设置超出边框裁剪为Yes
view.clipsToBounds = YES;

本文适合iOS开发初学者阅读,大牛们请一笑而过,如果有错误请联系我 。

如果您喜欢这篇文章,请关注我,喜欢或者打赏!您的支持十分重要!

iOS-UI控件精讲之UIView的更多相关文章

  1. iOS UI控件继承关系图

    闲来无事,把UI控件的继承关系图整理下来,供自己和大家使用.

  2. iOS-UI控件精讲之UILabel

    UILabel(标签)应该是iOS中最基本的一个控件了,也是使用频率最高的,经常用来展示一段不可编辑的文本. UILabel继承于UIView,下面是一些常用的属性,包含继承于UIView的属性. 1 ...

  3. ios UI控件的简单整理

    把该文件拷贝到.m文件中就能够方便的查找 /** 匿名类目:能够声明方法和变量,属性为private(不同意在外部调用,且不能被继承 */ /** 发送数据的托付方,接收数据的时代理发(即代理的反向传 ...

  4. iOS UI控件总结(全)

    1.UIButton UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btn.frame = CGRectMake ...

  5. Qt控件精讲一:按钮

    原地址:http://blog.csdn.net/yuxikuo_1/article/details/17397109 Qt Creater提供6种Button控件.如图1. Button控件介绍 控 ...

  6. iOS UI控件

    创建: 2018/04/21 完成: 2018/04/25 更新: 2018/09/24 补充UIActivityIndicatorView的显示和隐藏方法 UIButton  设定项目  项目名   ...

  7. iOS UI控件之间的关系图

  8. IOS学习资源收集--开发UI控件相关

    收集的一些本人了解过的iOS开发UI控件相关的代码资源(本文持续补充更新) 内容大纲: 1.本人在github上也上传了我分装好的一些可重复利用的UI控件 2.计时相关的自定义UILabel控件 正文 ...

  9. iOS 使用UI控件的外观协议UIAppearance进行设置默认UI控件样式

    在iOS开发中,经常会对UINavigationBar的样式进行全局样式.采用的设置方式有两种: 第一种,采用方式如下: [UINavigationBar appearance] 这种是对一类对象的默 ...

随机推荐

  1. java List遍历的方法

    List可以用序号来遍历,但通常推荐使用iterator来遍历Iterator itr = list.iterator();while (itr.hasNext()) { Object nextObj ...

  2. Intersoft Mobile Studio 2013 R1 SP1 Crack

    Intersoft Mobile Studio 2013 R1 SP1 (iOS, Android & WinR) Leave a comment   tweet       inShare ...

  3. Java Web SSH框架总是无法写入无法读取Cookie

    不关乎技术,关乎一个小Tips: 默认情况下,IE和Chrome内核的浏览器会认为http://localhost为无效的域名,所以不会保存它的cookie,使用http://127.0.0.1访问程 ...

  4. Ubuntu 下安装Kibana和logstash

    原文地址:http://www.cnblogs.com/saintaxl/p/3946667.html 简单来讲他具体的工作流程就是 logstash agent 监控并过滤日志,将过滤后的日志内容发 ...

  5. Ubuntu----1

    1. 安装ubuntu之后,你必须要做的事情, 引自:http://itsfoss.com/things-to-do-after-installing-ubuntu-13-04/ 但是对于国人来讲,墙 ...

  6. QEMU MIPIS远程共享ubuntu主机的文件

    尝试啦很多中办法,最终选择以在QEMU模拟器中ssh 远程登入的方式访问主机,并用sshfs 挂载 主机端的文件到模拟器中,实现模拟器访问主机端的代码. SSH分客户端openssh-client和o ...

  7. Implement the hash table using array / binary search tree

    今天在复习Arrays and String 时看到一个很有趣的问题.希望跟大家分享一下. Implement the hash table using array / binary search t ...

  8. 【转】git - 简明指南

    git - 简明指南 助你入门 git 的简明指南,木有高深内容 ;) 作者:罗杰·杜德勒 感谢:@tfnico, @fhd 和 Namics其他语言 english, deutsch, españo ...

  9. radix树

    今天在学Linux内核页高速缓存时,学到了一种新的数据结构radix树(基数),经过分析,感觉此数据结构有点鸡肋,有可能是我理解不到位吧. 先来张图,给大家以直观的印象 当有一个key-value型的 ...

  10. ASP.NET MVC- Controllers and Routing- Controller Overview

    In this tutorial, Stephen Walther introduces you to ASP.NET MVC controllers. You learn how to create ...