The difference between the frame, bounds, and center attributes of iOS development UIView (drawings)
Source: Internet
Author: User
<span id="Label3"></p><p><p>Blog temporarily think of what to write, not logical, not shun chapter into the Article.</p></p><p><p>First look at a few concepts</p></p><p><p>Coordinate point poit: the right side is <span class="s1">the x-axis positive direction of the value x, the lower side of the origin is the y-axis positive direction of the value y</span></p></p><p><p>Size: composed of width and height heights, representing a rectangle</p></p><p><p>Region Rect: It has coordinate points poit and size sizes, representing an area, both position and size</p></p><p><p>Relative: there is a reference, because the size of the reference position change and change</p></p><p><p>Absolute: no reference, fixed size position</p></p><p class="p1"><p class="p1"><em>Then look at the code composition:</em></p></p><p class="p1"><p class="p1"><span class="s1">Points are created by this, x-axis size and y size <span class="s1">cgpoint point = Cgpointmake (80,40)</span></span></p></p><p class="p1"><p class="p1"><em>sizes size is created by this</em> <span class="s1">cgsize size = Cgsizemake (144,72) means creating a rectangular view with a width of 144 and a height of 72</span></p></p><p class="p1"><p class="p1">the <em>Zone rect is created like This:</em> <span class="s1">cgrect rect=cgrectmake (10, 10, 120, 100); It represents a view of position at point (10,10) width 120 height 100</span></p></p><p><p>And then look at the definition description of frame, bounds and Center.</p></p><p class="p1"><p class="p1"><span class="s1">Frame: describes the position and size of the current view in its parent view</span></p></p><p class="p1"><p class="p1"><span class="s1">Bounds: describes the position and size of the current view in its own coordinate system.</span></p></p><p class="p1"><p class="p1"><span class="s1">Center: describes the position of the center point of the current view in its parent view.</span></p></p><p class="p1"><p class="p1">as <span class="s1">we can see from the description, the frame and bounds properties both describe the size of the view (cgsize) and the position (cgpoint), both of which are represented by Cgrect. The difference is that the frame describes the CGRect in its parent view, and bounds describes the CGRect in its own view, that is, the coordinates of the two are Different.</span> the center represents the position of the <em style="line-height: 1.5;"><span class="s1">Rectangle's Central Point in its parent view. </span></em> <span class="s1">frame is typically used to set the size and position of the view, and center to change the position of the VIEW. Both frame and center can change position, and if the view is rotated, zooming is also done relative to the Center. </span></p></p><p class="p1"><p class="p1">Most intuitive we use a graph and code value to describe the three representations:</p></p><p class="p1"><p class="p1">first, Create an empty view, drag and drop three wiew to go in, give different colors, and name them respectively. The code and diagram are as follows</p></p><pre><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">ViewController.h</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">nspredicatetest</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Created by Xuhongjiang on 15/10/27.</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Copyright (c) 2015 xuhongjiang. All Rights Reserved.</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">#import</span></span><UIKit/UIKit.h><span style="color: #0000ff;"><span style="color: #0000ff;">@interface</span></span><span style="color: #000000;"><span style="color: #000000;">viewcontroller:uiviewcontroller@property (retain, nonatomic) iboutlet UIView</span></span>*<span style="color: #000000;"><span style="color: #000000;">orangeview, @property (retain, nonatomic) iboutlet UIView</span></span>*<span style="color: #000000;"><span style="color: #000000;">greenview, @property (retain, nonatomic) iboutlet UIView</span></span>*<span style="color: #000000;"><span style="color: #000000;">grayview;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@end</span></span></pre><p><p></p></p><p class="p1"><p class="p1"><em>Three view, orange view max, with gray view and green view are defined Respectively. Let's take green view to illustrate the Problem.</em></p></p><p class="p1"><p class="p1"><em>1.frame</em></p></p><p class="p1"><p class="p1">in <em>fact, the size of the gray view is the green view frame size, green views Frame</em> the <span class="s1">origin point (the width of the gray view, the height of the gray View) is also the point of the gray view in relation to the lower-right corner of the Orange view, as well as the upper-left corner of the green view relative to the orange VIEW. Frame but size is the width and height of the green view</span></p></p><p class="p1"><p class="p1"><span class="s1">2.bounds</span></p></p><p class="p1"><p class="p1">The bounds point of the green view is relative to itself, is (0,0), size is the width and height of the green view, this is easy to understand.</p></p><p class="p1"><p class="p1">3.center</p></p><p class="p1"><p class="p1">The horizontal axis of the center of the green view = (green View width width/2) + (green view's. frame.origin.x); ordinate = (green View height height/2) + (green view. frame.origin.y);</p></p><p class="p1"><p class="p1">The following code is after debugging the specific value, careful observation, it is not difficult to find the <em>frame, bounds, Center</em> of the difference between the three or very obvious.</p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">VIEWCONTROLLER.M</span></span><span style="color: #008080;"><span style="color: #008080;">3</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">nspredicatetest</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008080;"><span style="color: #008080;">5</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Created by Xuhongjiang on 15/10/27.</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Copyright (c) 2015 xuhongjiang. All Rights Reserved.</span></span><span style="color: #008080;"><span style="color: #008080;">7</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span> <span style="color: #008080;"><span style="color: #008080;">9</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">#import</span></span> <span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">ViewController.h</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #008080;"><span style="color: #008080;">Ten</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">#import</span></span> <span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">Products.h</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span> <span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #0000ff;"><span style="color: #0000ff;">@interface</span></span><span style="color: #000000;"><span style="color: #000000;">Viewcontroller ()</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">@end</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">@implementation</span></span><span style="color: #000000;"><span style="color: #000000;">Viewcontroller</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Viewdidload {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #000000;"><span style="color: #000000;">[super viewdidload];</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">[self laytest];</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #000000;"><span style="color: #000000;">}</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #008080;"><span style="color: #008080;"></span> at</span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) didreceivememorywarning {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">[super didreceivememorywarning];</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">}</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span>-(<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Laytest</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">{</span></span><span style="color: #008080;"><span style="color: #008080;"></span> in</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">The frame of the <span style="color: #008000;">Orange View Returns a range of CGRect relative to the parent container, consisting of a dot point and size</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>CGRect orangeframerect=_orangeview.frame;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">(cgrect) orangerect = (origin = (x = +, y = 98), size = (width = 289,height = 443))</span></span><span style="color: #008080;"><span style="color: #008080;"></span> to</span>Cgpoint orangeframepoit=_orangeview.frame.origin;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">(cgpoint) orangepoit = (x = +, y = 98)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span>Cgsize orangeframesize=_orangeview.frame.size;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">(cgsize) orangesize = (width = 289, height = 443)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">the <span style="color: #008000;">Orange View's Bounds returns the position and size in its own coordinates, as well as an area cgrect, which is also made up of point points and size sizes</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>CGRect orangeboundsrect= _orangeview.bounds;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">(cgrect) orangeboundsrect = (origin = (x = 0, y = 0), size = (width = 289, height = 443))</span></span><span style="color: #008080;"><span style="color: #008080;"></span> *</span>Cgpoint orangeboudspoit=_orangeview.bounds.origin;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">(cgpoint) orangeboudspoit = (x = 0, y = 0)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> $</span>Cgsize orangeboundssize=_orangeview.bounds.size;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">(cgsize) orangeboundssize = (width = 289, height = 443)</span></span><span style="color: #008080;"><span style="color: #008080;">Panax Notoginseng</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">center <span style="color: #008000;">of Orange view it represents the self-centered point relative to the parent container</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>Cgpoint oranagecenterpoit= _orangeview.center;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">(cgpoint) oranagecenterpoit = (x = 187.5, y = 319.5)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">The green view frame is equivalent to the gray view area, which returns an area CGRect relative to the parent container (orange view), consisting of a dot point and size</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span>CGRect greenframerect=_greenview.frame;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">(cgrect) greenframerect = (origin = (x = all, y = +), size = (width = 209, height = max))</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>Cgpoint greenframepoit=_greenview.frame.origin;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">(cgpoint) greenframepoit = (x = +, y = +)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> $</span>Cgsize greenframesize=_greenview.frame.size;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">(cgsize) greenframesize = (width = 209, height = +)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> $</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">The bounds of the <span style="color: #008000;">green view returns the position and size in its own coordinates, and is also an area cgrect, which is also made up of points point and size</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>CGRect greenboundsrect= _greenview.bounds;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">(cgrect) greenboundsrect = (origin = (x = 0, y = 0), size = (width = 209, height = +))</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>Cgpoint greenboudspoit=_greenview.bounds.origin;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">(cgpoint) greenboudspoit = (x = 0, y = 0)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>Cgsize greenboundssize=_greenview.bounds.size;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">(cgsize) greenboundssize = (width = 209, height = +)</span></span><span style="color: #008080;"><span style="color: #008080;">Wuyi</span></span> <span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">center <span style="color: #008000;">of Green view it represents the self-centered point relative to the parent container (orange view)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>Cgpoint greencenterpoit= _greenview.center;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">(cgpoint) greencenterpoit = (x = 144.5, y = 192)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> wu</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">The gray view area is equivalent to the green view frame x=width=40 y=height=64</span></span><span style="color: #008080;"><span style="color: #008080;"></span> about</span>Cgsize grayframepoit=_grayview.frame.size;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">(cgsize) grayframepoit = (width = max, height = max)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> $</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Why is it only 20 and 32 points because it is relative to the Green Container's own center point</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>Cgpoint graycenterpoit= _grayview.center;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">(cgpoint) graycenterpoit = (x =, y = +)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>}</pre><p><p>This article is a personal original, welcome criticism, such as reproduced please indicate the SOURCE.</p></p><p><p>The difference between the frame, bounds, and center attributes of iOS development UIView (drawings)</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service