ios view的frame和bounds之区别(位置和大小)
转载自 http://blog.csdn.net/mad1989/article/details/8711697
前言:
学习ios开发有一段时间了,项目也做了两个了,今天看视频,突然发现view的frame和bound两个属性,发现bound怎么也想不明白,好像饶你了死胡同里,经过一番尝试和思考,终于弄明白bound的含义。PS:我承认我是一个很笨很笨的人。
所以现在记录下来,供以后查阅,同时方便所有和我一样有疑惑的人查看。
一、首先列一下公认的资料:
先看到下面的代码你肯定就明白了一些:
-(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);
}
很明显,bounds的原点是(0,0)点(就是view本身的坐标系统,默认永远都是0,0点,除非认为setbounds),而frame的原点却是任意的(相对于父视图中的坐标位置)。
再来看张图就明白了,
frame: 该view在父view坐标系统中的位置和大小。(参照点是,父亲的坐标系统)
bounds:该view在本地坐标系统中的位置和大小。(参照点是,本地坐标系统,就相当于ViewB自己的坐标系统,以0,0点为起点)
center:该view的中心点在父view坐标系统中的位置和大小。(参照电是,父亲的坐标系统)
我个人认为,bounds稍微有点费解,稍不留神,想的多了,就会绕 进去。每个view都有一个本地坐标系统。这个坐标系统作用比较重要,比如触 摸的回调函数中的UITouch里面的>坐标值都是参照这个本地坐标系统的坐标。当然bounds这个属性也是参照这个本地坐标系统来的。其实本地 坐标系统的关键就是要知道的它的原点(0,0)在什么位置(这个位置又是相对于上层的view的本地坐标系统而言的,当然最上面的一层view就是 window它的本地坐标系统原点就是屏幕的左上角了)。通过修改view的bounds属性可以修改本地坐标系统的原点位置。
所以,我个人认为,bounds影响到子view的位置和大小。
二、demo演示:
- UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 280, 250)];
- [view1 setBounds:CGRectMake(-20, -20, 280, 250)];
- view1.backgroundColor = [UIColor redColor];
- [self.view addSubview:view1];//添加到self.view
- NSLog(@"view1 frame:%@========view1 bounds:%@",NSStringFromCGRect(view1.frame),NSStringFromCGRect(view1.bounds));
- UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
- view2.backgroundColor = [UIColor yellowColor];
- [view1 addSubview:view2];//添加到view1上,[此时view1坐标系左上角起点为(-20,-20)]
- NSLog(@"view2 frame:%@========view2 bounds:%@",NSStringFromCGRect(view2.frame),NSStringFromCGRect(view2.bounds));
(运行展示,图中说的很明白了哦)
(log输出日志表明,每个新的view默认的bounds其实都是(0,0))
相关推荐
iOS view的frame和bounds之区别.pdf
默认UITableView是UITableViewStyleGrouped组模式,Frame是self.view.bounds的大小,继承者可重写setBaseTableView方法修改TableView和添加tableHeaderView和tableFooterView等属性! 内部提供可以跳转Model和基本...
view.frame = CGRectMake(0, self.view.bounds.size.height-240, self.view.bounds.size.width, 240); //view.dateLimitNum = 0; view.hoursType = UIHoursTypeWork; view.minutesType = UIMinutesTypeAll; ...
使用Swift,封装了一个不一样的GearRefreshControl,继承自UIRefreshControl。...,初始化:refreshControl = GearRefreshControl(frame:self.view.bounds).使用方法refreshControl.addTarget()即可。
1、初始化 drawMarqueeView = WQScrollLabelView(frame: CGRect(x: 0, y: 104, width: self.view.bounds.width, height: 20)) 2、设置属性 drawMarqueeView.marqueeDirection = .left view.addSubview...
//一般使用bounds不设置x和y只设置宽和高 //center是相对于其父视图而言的,是CGpoint类型 _vi.bounds=CGRectMake(0, 0, 200, 200); // vi.center=CGPointMake(160, 240); _vi.center=_window.center; //arc4...
MLHGridView *grid = [[MLHGridView alloc]initWithFrame:CGRectMake(10, 64, self.view.frame.size.width-20, CGRectGetHeight(self.view.bounds) - 64)]; grid.delegate = self; 2、实现delegate (NSInteger)...
The Window 4 Experimenting With Views 7 Subview and Superview 8 Visibility and Opacity 11 Frame 12 Bounds and Center 13 Window Coordinates and Screen Coordinates 17 Transform 18 Trait Collections and ...
let collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout) 可以设置的属性: //cell的尺寸 var itemSize: CGSize //cell间距 var spacing: CGFloat //缩放率 var ...
let loadingView = UILoadingView(frame: self.view.bounds) // show the loading view self.view.addSubview(loadingView) 您还可以指定要使用的自定义文本而不是“正在加载...” let loadingView = ...
前言 以前不用自己切图,现在要自己切图,看到设计稿有好多不同规格的渐变色的背景,一个一个切的话好麻烦,没有想到iOS本来就...gradientLayer.frame = view.bounds; [gradientLayer setColors:[NSArray arrayWithObje
手写控件,frame,center和bounds属性 一、手写控件 1.手写控件的步骤 (1)使用相应的控件类创建控件对象 (2)设置该控件的各种属性 (3)添加控件到视图中 (4)如果是button等控件,还需考虑控件的单击事件等 ...
TFSButton* btn = [[TFSButton alloc]initWithFrame:CGRectMake((self.view.bounds.size.widt h - 180)/2, 200, 180, 40) touchBlock:^(TFSButton *btn) { // 向服务器请求验证码 }]; [self.view addSubview:...
Xamarin.iOS.iCarousel 这是库的Xamarin iOS绑定。... Bounds = View . Bounds , ContentMode = UIViewContentMode . Center , Type = iCarouselType . CoverFlow2 , Frame = View . Frame , CenterItemWhen
let customView = CustomSliderView.init(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 300), type: SliderViewSourceType.SourceTypeLocalSource, imageArray: imageArray, sliderTime: 3.0) ...
self.controllerScrollview = [ControllerScrollView.alloc initWithFrame:CGRectMake(0, self.topScrollview.frame.origin.y self.topScrollview.frame.size.height, [UIScreen mainScreen].bounds.size.width, ...
退回输入键盘 2 CGRect 2 CGPoint & CGSize 3 设置透明度 3 设置背景色 3 自定义颜色 3 竖屏 3 横屏 3 ...iPhone里的frame和bounds区别 43 Objective-C内存管理 44 iphone更改键盘右下角按键的type 45
focusView3.frame = CGRectMake(20, 20, [UIScreen mainScreen].bounds.size.width-40, [UIScreen mainScreen].bounds.size.height-40); [self.view addSubview:focusView3]; focusView3.delegate =self; ...
let frame = CGRectMake(0, 44, self.view.bounds.width, 70) let datePicker = KBHDatePicker(frame: frame) self.view.addSubview(datePicker) 执照 KBHDatePicker在MIT许可下可用。 有关更多信息,请参见LICENSE...