ios uiview 怎么使用

2025-05-19 14:33:05
推荐回答(1个)
回答1:

#import "ViewController.h"
@interface ViewController
()
@end
@implementation
ViewController
- (
void
)viewDidLoad {
[
super viewDidLoad
];

//UIView 所有控件的父类

// Do any additional setup after loading the view, typically from a nib.

//每个视图控制器都会携带一个占满屏幕view视图,这是系统自带的,我们自己创建的视图控件都要加载在这个view上,通过self.view来访问

self.view.backgroundColor=[UIColor greenColor
];

//创建uiview控件的实例对象

UIView* myView1=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 20)];//高度20 是固定的电池栏的高度

//设置myview1的背景颜色
myView1.
backgroundColor=[UIColor redColor];//红色

//两种方法 点语法和set方法
[myView1 setBackgroundColor:[UIColor grayColor]];//浅灰色
//添加
[self.view addSubview:myView1];

UIView* myView2=[[UIView alloc]initWithFrame:CGRectMake(50, 50, 100, 100
)];
[myView2
setBackgroundColor:[UIColor blackColor
]];
[
self.view addSubview
:myView2];

UIView* myView3=[[UIView alloc]initWithFrame:CGRectMake(70, 70, 70, 70
)];
[myView3
setBackgroundColor:[UIColor blueColor
]];
[
self.view addSubview
:myView3];

//把myview3放到最下层 视图
[
self.view sendSubviewToBack
:myView3];

//把myview3放到最上层
[
self.view bringSubviewToFront
:myView3];

//sunviews 获取视图中所有的子视图;

NSLog(@"--%@",self.view.subviews);

//通过索引值来调换两者的层次
// [self.view exchangeSubviewAtIndex:3 withSubviewAtIndex:4];
UIView* myView4=[[UIView alloc]initWithFrame:CGRectMake(68, 68, 75, 75
)];
[myView4
setBackgroundColor:[UIColor redColor
]];
[
self.view addSubview
:myView4];

[
self.view insertSubview:myView4 atIndex:4
];

//移除某个视图
[myView1
removeFromSuperview
];
}
- (
void
)didReceiveMemoryWarning {
[
super didReceiveMemoryWarning
];

// Dispose of any resources that can be recreated.
}