标签:
1、UIWindow
App靠window来呈现内容,?个程序?般只创建?个window。
通常window的??(frame)与屏幕(UIScreen)???致。 ?例代码如下:
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
_window.backgroundColor = [UIColor whiteColor];
// 视图的添加写在这里
[_window makeKeyAndVisible];
[_window release];
2、UIView
UIView是所有可视化控件的基类。
创建视图的步骤
//1、开辟空间并初始化
UIView * blueView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
//2、对视图进行设置
blueView.backgroundColor = [UIColor blueColor];
//3、将视图添加到window来显示
[_window addSubview:blueView];
//4、释放视图对象
[blueView release];
2.1.frame
iOS坐标系
?平向右:为x的正?向
垂直向下:为y的正?向
坐标系不是以像素作为划分依据,?是以“点”作为依据
2.2.bounds(与frame相反)
?平向左:为x的正?向
垂直向上:为y的正?向
2.3.center
center.x = frame.origin.x+frame.size.length/2, frame.origin.y+frame.size.height/2;
故,center的值只与frame有关,修改bounds并不会影响center
2.4.难区分的bounds与frame
此图仅供参考,与代码无关,转载自:http://blog.csdn.net/mad1989/article/details/8711697
从以下代码可以看出:frame的位置和大小跟其父类视图有关,但bounds的原点固定(0,0)。另外,frame的大小与bounds的大小是一样的,修改两者任一个都会影响另一个。
-(CGRect)frame{
return CGRectMake(self.frame.origin.x,self.frame.origin.y,self.frame.size.width,self.frame.size.height);
}
-(CGRect)bounds{
return CGRectMake(0,0,self.frame.size.width,self.frame.size.height);
}
结论:
subview.frame.origin = superview.bounds.origin
即:子视图的frame的原点取决于父视图的bounds的原点。
代码:
UIView * redView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; // 此时redView.bounds默认为(0, 0, 100, 100);
// redView.bounds.origin.x = 10; ??
// 当对一个对象的结构体属性的结构体成员进行赋值的时候,不能直接赋值// 修改bounds
CGRect tempBounds = redView.bounds;
tempBounds.origin.x = -20;
tempBounds.origin.y = -20;
tempBounds.size.height = 200;
tempBounds.size.width = 200;
redView.bounds = tempBounds;
NSLog(@"bounds:%@, frame:%@", NSStringFromCGRect(redView.bounds), NSStringFromCGRect(redView.frame)); // 输出bounds|frame
redView.backgroundColor = [UIColor redColor];
[_window addSubview:redView];
[redView release];
UIView * blueView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
blueView.backgroundColor = [UIColor blueColor];
[redView addSubview:blueView];
[blueView release];
效果图:
2.5.数据结构
@property(nonatomic) CGRect frame;
@property(nonatomic) CGRect bounds; // default bounds is zero origin, frame size. animatable
@property(nonatomic) CGPoint center; // center is center of frame. animatable
@property(nonatomic) CGAffineTransform transform; // default is CGAffineTransformIdentity. animatable
//—————————CGRect
struct CGRect {
CGPoint origin;
CGSize size;
};
typedef struct CGRect CGRect;
//—————————CGPoint
struct CGPoint {
CGFloat x;
CGFloat y;
};
typedef struct CGPoint CGPoint;
//—————————CGSize
struct CGSize {
CGFloat width;
CGFloat height;
};
typedef struct CGSize CGSize;
//—————————CGFloat
typedef CGFLOAT_TYPE CGFloat;
center
@property(nonatomic) CGPoint center;
bounds
@property(nonatomic) CGRect bounds;
2.3.视图重要属性
// redView.hidden = YES; // 隐藏
redView.alpha = 0.333; //透明度,默认1.0
NSLog(@"%@", redView.superview); //superview
NSLog(@"%@", _window.subviews); //subviews
// 通过设置tag来访问view
grayView.tag = 101;
UIView * view = [_window viewWithTag:101];
NSLog(@"view:%@", view);
4、视图操作
添加视图
[_window addSubview:redView];
[_window insertSubview:grayView atIndex:0];
管理视图层次
[_window bringSubviewToFront:redView];
[_window sendSubviewToBack:redView];
[_window exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
[redView removeFromSuperview];
5、UILabel(标签)
显??本的控件
使用示例:
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(100, 400, 200, 100)];
// 设置文本显示
label.text = @"Hello Lanou!can i kemaskjf a ekjrmf kznme n";
label.backgroundColor = [UIColor grayColor];
label.font = [UIFont fontWithName:@"Times New Roman" size:20];
NSLog(@"%@", [UIFont familyNames]);
label.textColor = [UIColor redColor];
label.textAlignment = NSTextAlignmentCenter;
// 设置换行
label.lineBreakMode = NSLineBreakByWordWrapping; //设置换行模式
label.numberOfLines = 3;
label.shadowColor = [UIColor purpleColor];
label.shadowOffset = CGSizeMake(2, 2);
[_window addSubview:label];
[label release];
标签:
原文地址:http://my.oschina.net/zooyf/blog/492574