iOS layout note One (the code implements its own active layout)

Source: Internet
Author: User
Tags uikit



1. Use a throttling condition to place an attempt at the central location of its parent view. 2. Create two restrictions: one is to arrange the center.x position of the target view in the center.x position of its parent view, and the other is to arrange the center.y position of the target view in the CENTER.Y position of its parent view.

3. First add a button to the WildCatViewController.h
////wildcatviewcontroller.h//AutoLayoutDemo// Created by Wildcat on 14-4-20.//Copyright (c) 2014 Com.wildcat. All rights reserved.//#import <UIKit/UIKit.h> @interface Wildcatviewcontroller:uiviewcontroller@property ( Nonatomic,strong) Uibutton*button; @end  
Implemented in. m Files:

wildcatviewcontroller.m//autolayoutdemo////Created by Wildcat on 14-4-20.//Copyright (c) 2014 Com.wildcat. All rights reserved.//#import "WildCatViewController.h" @interface Wildcatviewcontroller () @end @implementation Wildcatviewcontroller@synthesize button=_button;-(void) Viewdidload{[super Viewdidload];_button=[uibutton Buttonwithtype:uibuttontyperoundedrect];_button.translatesautoresizingmaskintoconstraints=no; [_button settitle:@ "Wildcat" forstate:uicontrolstatenormal]; [Self.view Addsubview:_button]; UIView *superview=_button.superview;//joins the constraint so that the button is centered nslayoutconstraint the horizontal direction of the screen *centerxcontraint=[ Nslayoutconstraintconstraintwithitem:_buttonattribute:nslayoutattributecenterxrelatedby: nslayoutrelationequaltoitem:superviewattribute:nslayoutattributecenterxmultiplier:1.0fconstant:0.0];//Join the constraint. Make the button central Nslayoutconstraint *centerycontraint=[nslayoutconstraintconstraintwithitem:_buttonattribute in the vertical direction of the screen: NSLayoutAttributeCenterYrelatedBy:NSLayoutRelationEqualtoItem:superViewattribute:NSLayoutAttributecenterymultiplier:1.0fconstant:0.0];//joins the constraint to the parent of the button [Superview addconstraints:@[centerxcontraint, Centerycontraint]];} -(Nsuinteger) Supportedinterfaceorientations{return uiinterfaceorientationmaskall;//screen can rotate}-(void) Didreceivememorywarning{[super didreceivememorywarning];//Dispose of any of the resources that can be recreated.} @end

Don't forget to change the device to rotate the direction.
Execution results for example with:

This article transferred from: http://1.wildcat.sinaapp.com/?p=42

The constraints and graphs for the views they want to join are as follows:

Reprint Please specify: All rights reserved Click to open the link

What to learn Next: iOS layout Note II (Visual Format Language defines horizontal and vertical constraints)



iOS layout note One (the code implements its own active layout)

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.