iOS view 之 frame 和 bounds

本文详细解析了iOS和Android平台下视图坐标系统的特点,包括原点定义、bounds与frame属性的区别,以及如何通过bounds和frame正确定位视图元素。通过实例演示了如何设置视图坐标,实现不同设备间的兼容性调整。

// animatable. do not use frame if view is transformed since it will not correctly reflect the actual location of the view. use bounds + center instead.

@property(nonatomic) CGRect            frame;


@property(nonatomic) CGRect            bounds;      // default bounds is zero origin, frame size. animatable


-(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);
}


1、bounds的原点是(0,0)点,是view本身的坐标系统,默认永远都是0,0点。

2、人为setbounds会改变其值,这时候就指定了自身view的起始点坐标为自身view的坐标系统的坐标值,eg:

[view1 setBounds:CGRectMake(-10, -10, 200 ,200)];这时候说明view的起点左边为(-10,-10)

UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[view1 addSubview:view2];

把view2添加到view1,此时view1坐标系左上角起点为(-10,-10)],所有view2会相对于view1向右向下各偏移10个点

3、frame的原点是任意的,它的原点是相对于父视图中的坐标位置。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值