`
stephen830
  • 浏览: 2966782 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

ios view的frame和bounds之区别(位置和大小)

 
阅读更多

 

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演示:

 

  1. UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 280, 250)];  
  2. [view1 setBounds:CGRectMake(-20, -20, 280, 250)];  
  3. view1.backgroundColor = [UIColor redColor];  
  4. [self.view addSubview:view1];//添加到self.view  
  5. NSLog(@"view1 frame:%@========view1 bounds:%@",NSStringFromCGRect(view1.frame),NSStringFromCGRect(view1.bounds));  
  6.   
  7. UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];  
  8. view2.backgroundColor = [UIColor yellowColor];  
  9. [view1 addSubview:view2];//添加到view1上,[此时view1坐标系左上角起点为(-20,-20)]  
  10. NSLog(@"view2 frame:%@========view2 bounds:%@",NSStringFromCGRect(view2.frame),NSStringFromCGRect(view2.bounds));  




 

(运行展示,图中说的很明白了哦)

 

 

 



 

 

(log输出日志表明,每个新的view默认的bounds其实都是(0,0))

 

 

 

 

  • 大小: 35.2 KB
  • 大小: 59.7 KB
  • 大小: 28.9 KB
分享到:
评论

相关推荐

    iOS view的frame和bounds之区别.pdf

    iOS view的frame和bounds之区别.pdf

    ios-国际化和本地化(支持英语单复数问题).zip

    默认UITableView是UITableViewStyleGrouped组模式,Frame是self.view.bounds的大小,继承者可重写setBaseTableView方法修改TableView和添加tableHeaderView和tableFooterView等属性! 内部提供可以跳转Model和基本...

    ios-时间选择器.zip

    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; ...

    ios-DMRefreshControl.com

    使用Swift,封装了一个不一样的GearRefreshControl,继承自UIRefreshControl。...,初始化:refreshControl = GearRefreshControl(frame:self.view.bounds).使用方法refreshControl.addTarget()即可。

    ios-一款简单好用的跑马灯效果-swift.zip

    1、初始化 drawMarqueeView = WQScrollLabelView(frame: CGRect(x: 0, y: 104, width: self.view.bounds.width, height: 20)) 2、设置属性 drawMarqueeView.marqueeDirection = .left view.addSubview...

    ios开发记录

    //一般使用bounds不设置x和y只设置宽和高 //center是相对于其父视图而言的,是CGpoint类型 _vi.bounds=CGRectMake(0, 0, 200, 200); // vi.center=CGPointMake(160, 240); _vi.center=_window.center; //arc4...

    iOS-GridView:iOS自定义网格组件,基于UICollectionView的GridView

    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)...

    Programming.iOS.9

    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 ...

    ios-卡片布局-Swift.zip

    let collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout) 可以设置的属性: //cell的尺寸 var itemSize: CGSize //cell间距 var spacing: CGFloat //缩放率 var ...

    UILoadingView:使用 Swift 编写的 iOS 全屏加载视图简单易用

    let loadingView = UILoadingView(frame: self.view.bounds) // show the loading view self.view.addSubview(loadingView) 您还可以指定要使用的自定义文本而不是“正在加载...” let loadingView = ...

    iOS中利用CAGradientLayer绘制渐变色的方法实例

    前言 以前不用自己切图,现在要自己切图,看到设计稿有好多不同规格的渐变色的背景,一个一个切的话好麻烦,没有想到iOS本来就...gradientLayer.frame = view.bounds; [gradientLayer setColors:[NSArray arrayWithObje

    iOS开发中一些手写控件及其相关属性的使用

    手写控件,frame,center和bounds属性 一、手写控件 1.手写控件的步骤 (1)使用相应的控件类创建控件对象 (2)设置该控件的各种属性 (3)添加控件到视图中 (4)如果是button等控件,还需考虑控件的单击事件等 ...

    ios-验证码设置.zip

    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:用于iOS的简单,高度可定制的,数据驱动的3D旋转木马绑定Xamarin.iOS

    Xamarin.iOS.iCarousel 这是库的Xamarin iOS绑定。... Bounds = View . Bounds , ContentMode = UIViewContentMode . Center , Type = iCarouselType . CoverFlow2 , Frame = View . Frame , CenterItemWhen

    ios-ZJGenWoYou.zip

    let customView = CustomSliderView.init(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 300), type: SliderViewSourceType.SourceTypeLocalSource, imageArray: imageArray, sliderTime: 3.0) ...

    ios-仿优酷视频,搜狐视频,凤凰新闻上导航滑动scrollview 以及绑定下面的viewcontroller.zip

    self.controllerScrollview = [ControllerScrollView.alloc initWithFrame:CGRectMake(0, self.topScrollview.frame.origin.y self.topScrollview.frame.size.height, [UIScreen mainScreen].bounds.size.width, ...

    iphone开发笔记

    退回输入键盘 2 CGRect 2 CGPoint & CGSize 3 设置透明度 3 设置背景色 3 自定义颜色 3 竖屏 3 横屏 3 ...iPhone里的frame和bounds区别 43 Objective-C内存管理 44 iphone更改键盘右下角按键的type 45

    轮播3d旋转

    focusView3.frame = CGRectMake(20, 20, [UIScreen mainScreen].bounds.size.width-40, [UIScreen mainScreen].bounds.size.height-40); [self.view addSubview:focusView3]; focusView3.delegate =self; ...

    KBHDatePicker:无限滚动日期选择器,类似于iOS的“日历”应用

    let frame = CGRectMake(0, 44, self.view.bounds.width, 70) let datePicker = KBHDatePicker(frame: frame) self.view.addSubview(datePicker) 执照 KBHDatePicker在MIT许可下可用。 有关更多信息,请参见LICENSE...

Global site tag (gtag.js) - Google Analytics