UI概述
UI(User Interface):用户界面,用户能看到的各种各样的页面元素。
iOS App = 各种各样的UI控件 + 业务逻辑和算法
在iOS中,使用UIWindow类来表示窗口,通常一个应用程序只创建一个UIWindow对象
window的主要作用是呈现内容给用户,我们不会对window做太多的操作
frame是一个结构体,包含2部分内容:origin和size。
origin也是一个结构体,包含2部分内容:x和y。
size同样是一个结构体,包含2部分内容:width和height。 frame的origin和size是相对于⽗父视图来说的。
CGRrectMake()函数可以帮我们快速构造一个CGRect变量
center(中⼼心点)也是view重要的属性。
center是个结构体,包含2个部分:x和y。
center与frame有着密切的联系。
center.x = frame.origin.x + frame.size.width/2;
center.y = frame.origin.y + frame.size.height/2;
bounds(边界)也是view的重要属性,⽤用于定义⾃自⼰己的边界。它同
frame一样是一个CGRect结构体变量。 当一个view设置bounds时,会把⾃自⼰己当成一个容器,定义⾃自⼰己的边
界⼤大⼩小 以及 左上⾓角的初始坐标。
当⼦子视图添加到此视图时,会根据bounds指定的原点(0,0)计算 frame,⽽而⾮非左上⾓角。
下面是相关代码段
#import "AppDelegate.h"
@interface AppDelegate()
@end
@implementation AppDelegate
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictonary *)launchOptions
{
//创建
self.window = [[UIWindow alloc] init];
//设置位置和大小
self.window.fram = [[UIScreen mainScreen].bounds];
//设置颜色
self.window.backgroundColor = [UIColor whiteColor];
//self.window.backgroundColor = [UIColor colorWithRed:2 / 255.0 green:30 / 255.0 blue:200 / 255.0 alpha:1];//这也是设置颜色的方法,设置了View 的RGB值
//添加到屏幕上
[self.window makeKeyAndVisible];
//创建一个蓝色视图
UIView *blueView = [[UIView alloc] init];
blueView.frame = CGRectMake(100,100,100,100);
blueView.backgroundColor = [UIColor blueColor];
[self.window addSubview:blueView];
//创建一个绿色视图放在蓝色视图的下方
UIView *greenView = [[UIView alloc] initWithFrame:CGRectMake(100,200,100,100)];
greenView.backgroundColor = [UIColor greenColor];
//frame:以父视图的坐标系为基准
//修改frame,会影响自己的位置和大小
//如果想要挪动自身的位置:
//1.修改自身的fram
//2.修改父视图的bounds
//设置视图的中心点
UIView *centerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];
centerView.backgroundColor = [UIColor blueColor];
//设置自己的中心和window的中心对齐
centerView.center = self.window.center;
[self.window addSubview:centerView];
//插入一个视图
UIView *cyanView = [[UIView alloc] initWithFrame:CGRectMake(100,100,100,100)];
cyanView.backgrounColor = [UIColor cyanColor];
//插入视图,给父视图发送消息,把要插入的视图作为参数
cyanView.center = self.window.center;
[self.window insertSubview:cyanView.atIndex:0];
cyanView.tag = 100;
//移除视图:给需要移除的对象发送消息
[cyanView removeFromSuperview];
//把一个视图移到最上面
[self.window bringSubviewToFront:cyanView];
//把一个视图移到最下面
[self.window sendSubviewToBack:cyanView];
//交换
[self.window exchangeSubviewAtIndex:1 withSubviewAtIndex:0];
//UIView 的常用属性
//cyanView.hidden = YES;
//cyanView.alpha = 0.1;
[self test];
//创建一个文本框
UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,20,100,30)];
//设置文本内容
nameLabel.text = @"";//写上你想写入的东西
//设置文本颜色
nameLabel.textColor = [UIColor redColor];
//让文本在window中居中
nameLabel.center = self.window.center;
//让文本在Label中居中(偏左,偏右,具体情况具体对待)
nameLabel.textAlignment = NSTextAlignmentCenter;
//将文本框添加到window上
[self.window addSubview:nameLabel];
return YES;
}
-(void)test
{
//给父视图发送消息,viewWithTag:根据tag的值,获取子视图
UIView *view = [self.window viewWithTag: 100];
view.backgroundColor = [UIColor redColor];
}