Absolute layout and relative layout of iOS development (screen fit)

Source: Internet
Author: User

There are also absolute and relative positioning in the UI design of iOS, and the absolute positioning and relative positioning of our web front ends are different but similar. Here's a combination of two small demos to learn about the absolute positioning and relative positioning of the UI in our iOS development. The UI cases used in the previous blog are all absolutely positioned, and the controls we storyboard are absolutely positioned, and we can change the position and size of the component with the frame of the component. Relative positioning is different, relative positioning is the element around the reference component to determine the size or position of the component, relative positioning is the constraint and the distance of the surrounding components to layout, that is, Layoutconstraint. Layoutconstraint and Fram layouts cannot coexist in the layout.

The following are described in both absolute and relative layouts using screen-fitting cases. The effect: When the view size and position of the above changes, in order not to overwrite the view below, we also want to change the location of the next view. Or in our 4.0-inch normal display of content, on the 3.5-inch screen can also be displayed normally, that is, usually we say the adaptation of the screen. To make it easier to observe, we can use the slider control to dynamically change the view fruit graph above:

1. Using absolute layout to achieve the above effect, in order to save our code writing time, the above control is implemented by Storyborad, and then in the corresponding Viewcontroller to add components and control callback method, The main point is to get the slider's value when the slider is sliding, and then dynamically set the frame coordinates of the view above (of course, if the view is extended around to calculate the value of the new Fram, and then dynamically modified), the view position and size of the above changes, Then the view below cannot be overwritten by the above, so you have to modify the value of the Blackview Fram. This is an absolute layout by modifying the value of the frame to determine the component position

Here are the properties dragged by storyboard:

Drag the top view to our code

@property (strong, nonatomic ) iboutlet UIView *myview;

Add Slider

@property ( strong nonatomic ) Span class= "crayon-t" >iboutlet uislider *myslider;

Add the following black view

@property (strong, nonatomic ) iboutlet UIView *blackview;

Here's how to callback when the value of the slider changes:

The callback method when the value of the slider changes

- (ibaction)sliderfunction:(ID)sender

{

Gets the current value of the slider (the range of storyboard settings is 0-120)

Double value = self. Myslider. Value;

Get the location of MyView

CGRect Frame = self. MyView. Frame;

Dynamically set the coordinates and width of the myview according to the value of the slider, setting the View center unchanged

Frame. Origin. X = + -value;

frame. Origin. Y = 66 * (1-value/ 120)

frame. Size. Height = 320-frame< Span class= "Crayon-sy". origin. X*2;

Frame. Size. Width = -frame. Origin. X*2;

Update the location of MyView

Self. MyView. Frame = frame;

Also change the coordinates of the black view below

cgrect bf = self. Blackview. Frame

BF. Origin. Y = frame. Size. Height + frame. Origin. Y + +;

Self. Blackview. Frame = BF;

}

2. The above is our absolute layout of the way, next to learn the relative layout of the way. Relative layout is more complex than the absolute layout, the following first screen adaptation of the example, figure one is on the iphone 4.0-inch, when we do not do any processing on the 3.5-inch screen is not shown as the second picture:

(1) How to let the 3.5-inch screen also display normal, and then the relative layout of the appearance of the time, we use the relative layout of the bottom of the view to the location of the image relative to the base and left of the pixel value fixed, while setting the slider position relative to the view below the position of the relative fixed. That is, the position of the veiw below is changed, the position of the slider above will also change, with storyboard modified as follows: (the first picture is to modify the bottom view of the relative position, the second picture is to set our slider for the relative layout), Don't need to add any dynamic in Viewcontroller, we can implement screen adaptation.

(2) Then how do I use the relative layout to achieve the above view magnification effect, next we need to create a new project, because the relative layout and absolute layout in the same component can not coexist. Using storyboard to drag and drop the controls we use in the new project, the interface is the same as above.

(1) First give us the top view to set the relative layout of the properties, such as the following figure a

(2) Set the relative layout properties to the Black view, as shown in Figure two below:

(3) Set the above two view relative center alignment, select the view above, press CTRL to the following view, drag, in the popup box, select Center x in Figure three

(4). How to dynamically change the width and height of the top view when the corresponding component is added to the storyboard constraint? (That is, change the value of the horizontal and vertical constraints) the first part of the top view of the horizontal and vertical constraints from our storyboard in the top view of the constraints we want to use to drag into our Viewcontroller, The first graph is where the constraints are in storyboard, and the second graph adds the constraints to the Viewcontroller.

(5) At this point we use storyboard work has been done, programmers are not without knocking code, but also only serious code, programmers will grow. So here's the part of the code that we added in Viewcontroller. Absolute layout Change the coordinate value of frame directly, then how do we change the value of our constraint dynamically in the program? The following code will be used. What we're going to do is change the value of the slider in the Viewcontroller to alter the horizontal and vertical constraints of the top view, the related variables for the horizontal and vertical constraints we've dragged them over, Below you need to change the values of the horizontal and vertical constraints in the method of the slider callback.

The callback method when the value of the slider changes

- (ibaction)sliderfunction:(ID)sender

{

Gets the current value of the slider (the range of storyboard settings is 0-120)

Double value = self. Myslider. Value;

Get the location of MyView

CGRect Frame = self. MyView. Frame;

Dynamically set the coordinates and width of the myview according to the value of the slider, setting the View center unchanged

Frame. Origin. X = + -value;

Frame. Origin. Y = * (1-value/+);

frame. Size. Height = 320-frame< Span class= "Crayon-sy". origin. X*2;

Frame. Size. Width = -frame. Origin. X*2;

Update the location of MyView

Self. MyView. Frame = frame;

Also change the coordinates of the black view below

cgrect bf = self. Blackview. Frame

BF. Origin. Y = frame. Size. Height + frame. Origin. Y + +;

Self. Blackview. Frame = BF;

}

1. There can only be one constraint in a component, such as in MyView we already have a vertical constraint, if we add a vertical constraint to him, then the program will be error when running, the wrong content: "Unable to simultaneously satisfy Constraints ... "; Code Description:

2. So before adding a new constraint, we have to remove the corresponding constraint previously added to our component; The constraint is added to the parent view of our corresponding component and removed from the parent view of the component;

3. When setting the value of a constraint, we pass the argument to the constraint as a string, such as: H:[_myview (+)] H for the horizontal constraint, and V for the vertical constraint. In brackets is the number of constraints and constraints we want to add to that component;

4. Update our constraint to our new constraint;

5. To add the updated constraint to our parent view, we can implement the above features we implemented with absolute layout above

Additional notes:

In the absolute layout we can also get the size of the screen, the size of the screen to calculate the location of our components, the main code is as follows:

//Get screen size

uiscreen *s< Span class= "crayon-h" > = [uiscreen mainscreen

//Get screen boundaries

cgrect bounds = Span class= "crayon-v" >s. Bounds

//Get the height of the screen

float height = Bounds. Size. Height

Absolute layout and relative layout of iOS development (screen fit)

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.