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)