UIView 基本属性1
UIView 介绍:
UIView 基本属性介绍
UIWindow:
CGRectMake
#import "ccyAppDelegate.h"
@implementation ccyAppDelegate
- (void)dealloc
{
//还没写代码就运行的白色界面就是UIWindow
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
/*
3gs - 320*480(分辨率)
4,4s - 640*960
但是上面这手机屏幕比例/尺寸没有改变,改变是密度
在supporting files中有两个图:
Default.png(320*480)
Default@2x.png(640*960)
两台手机程序不变,只要图片添加一个高分辨力的图片+@2x,这样就可以了。
5 - 640*1136 (最重要是屏幕比例变了)以后讲解
*/
UIView * view1 = [[UIView alloc] init];
view1.frame = CGRectMake(10, 30, 300, 30);
view1.backgroundColor = [UIColor redColor];
[self.window addSubview:view1];
[view1 release];
UIView * view2 = [[UIView alloc] init];
view2.frame = CGRectMake(10, 20, 300, 30);
view2.backgroundColor = [UIColor blueColor];
//将view2添加到父view1中 CGRectMake是相对于 view1左上角相对位置
[view1 addSubview:view2];
[view2 release];
NSLog(@"frame - x = %f", view2.frame.origin.x);
NSLog(@"frame - y = %f", view2.frame.origin.y);
NSLog(@"frame - w = %f", view2.frame.size.width);
NSLog(@"frame - h = %f", view2.frame.size.height);
/*
frame - x = 10.000000
frame - y = 20.000000
frame - w = 300.000000
frame - h = 30.000000
*/
//等到view2的中心点
//CGPoint 是一个类 所以不能 加 * 号
CGPoint point = view2.center;
NSLog(@"point - x = %f", point.x);
NSLog(@"point - y = %f", point.y);
/*
point - x = 160.000000
point - y = 35.000000
*/
//得到边框大小 bounds不能得到x和y
CGRect bounds = view2.bounds;
NSLog(@"bounds - x = %f", bounds.origin.x);
NSLog(@"bounds - y = %f", bounds.origin.y);
NSLog(@"bounds - w = %f", bounds.size.width);
NSLog(@"bounds - h = %f", bounds.size.height);
/*
bounds - x = 0.000000
bounds - y = 0.000000
bounds - w = 300.000000
bounds - h = 30.000000
*/
//设置视图标签
view2.tag = 2;
//设置内容模式 后面imageVIew 会讲解
// view2.contentMode =
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
UIView 基本属性2【视图自动缩放】
UIView 基本属性介绍:
#import "ccyAppDelegate.h"
@implementation ccyAppDelegate
{
UIView * _backView;
}
- (void)dealloc
{
[_backView release];
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
//例1
UIView * view1 = [[UIView alloc] init];
view1.frame = CGRectMake(10, 30, 300, 30);
view1.backgroundColor = [UIColor redColor];
[self.window addSubview:view1];
[view1 release];
UIView * view2 = [[UIView alloc] init];
view2.frame = CGRectMake(0, 5, 100, 20);
view2.backgroundColor = [UIColor blueColor];
[view1 addSubview:view2];
[view2 release];
UIView * view3 = [[UIView alloc] init];
view3.frame = CGRectMake(100, 5, 100, 20);
view3.backgroundColor = [UIColor grayColor];
[view1 addSubview:view3];
[view3 release];
//得到父类视图
UIView * superView = view2.superview;
superView.backgroundColor = [UIColor greenColor];
//得到子类视图数组
NSArray * subViews = view1.subviews;
NSLog(@"count = %d", subViews.count);
// count = 2
//for( UIView * view in subViews)
//{
// view.backgroundColor = [UIColor blueColor];
//}
//数组的顺序[最先加入到父视图的在数组中就是第1个]
UIView * view = [subViews objectAtIndex:0];
view.backgroundColor = [UIColor redColor];
//例2
UIView * blueView = [[UIView alloc] init];
blueView.frame = CGRectMake(10, 100, 300, 30);
blueView.backgroundColor = [UIColor blueColor];
[self.window addSubview:blueView];
[blueView release];
//自动剪裁功能 [如果子类视图超出父视图,会被剪裁 - 子类视图 小于等于 父亲视图]
blueView.clipsToBounds = YES;
UIView * greenView = [[UIView alloc] init];
greenView.frame = CGRectMake(10, 10, 100, 100);
greenView.backgroundColor = [UIColor greenColor];
[blueView addSubview:greenView];
[greenView release];
//设置透明度 [0 - 1] 0不透明
greenView.alpha = 0.5;
//例3
//自动布局
_backView = [[UIView alloc] init];
_backView.frame = CGRectMake(100, 200, 120, 120);
_backView.backgroundColor =[UIColor blackColor];
//准许子视图使用自动布局方式
_backView.autoresizesSubviews =YES;
[self.window addSubview:_backView];
UIView * topView = [[UIView alloc] init];
topView.frame = CGRectMake(10, 10, 100, 100);
topView.backgroundColor = [UIColor orangeColor];
//设置自动布局方式
/*
UIViewAutoresizingNone - none
//设置 左右,上下头可以改变
UIViewAutoresizingFlexibleLeftMargin -
UIViewAutoresizingFlexibleRightMargin
UIViewAutoresizingFlexibleTopMargin
UIViewAutoresizingFlexibleBottomMargin
//设置 宽 和 高 都和父视图同时改变
UIViewAutoresizingFlexibleWidth
UIViewAutoresizingFlexibleHeight
*/
topView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin;
[_backView addSubview:topView];
[topView release];
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timeTick) userInfo:nil repeats:YES];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
- (void)timeTick
{
_backView.frame = CGRectMake(_backView.frame.origin.x, _backView.frame.origin.y, _backView.frame.size.width+5, _backView.frame.size.height+5);
}
UIView 基本属性3【UIView 层操作】
UIView调整层的常用方法
获取层,并修改该层的颜色