IOS: Learning notes, code-driven automatic layout instances (Swift)

Source: Internet
Author: User

iOS auto-layout is a powerful tool for setting up the iOS interface.
This example shows how to use the automatic layout language to set the horizontal layout, vertical layout
1. Create a blank iOS project (Swift)
2. Add a controller class to modify the Yyappdelegate.swift file

@UIApplicationMainclassAppdelegate:uiresponder, uiapplicationdelegate {var window:uiwindow?func Application (application:uiapplication, didfinishlaunchingwithoptions launchoptions:nsdictionary?) -Bool {Self.window=UIWindow (Frame:UIScreen.mainScreen (). Bounds)//Override point for customization after application launch.self.window!. BackgroundColor =Uicolor.whitecolor () Self.window!. Makekeyandvisible () Self.window!. Rootviewcontroller =Mainviewcontroller (Nibname:nil, Bundle:nil)return true    }

3. Modifying the Controller class

////Mainviewcontroller.swift//Uibycode3_autolayout////Created by Yao_yu on 14-6-17.//Copyright (c) yao_yu. All rights reserved.//Import UIKitclassMainviewcontroller:uiviewcontroller {var viewmoveblock:uiview! =UIView () init (nibname nibnameornil:string?, bundle Nibbundleornil:nsbundle?) {super.init (Nibname:nibnameornil, Bundle:nibbundleornil)}Overridefunc viewdidload () {super.viewdidload () Self.viewMoveBlock.backgroundColor=Uicolor.bluecolor () self.viewMoveBlock.frame= CGRectMake ( -, -, -, -); Self.view.addSubview (Self.viewmoveblock) var commandpane= UIView (Frame:cgrectmake (0,0, the, +))//As UIViewSelf.view.addSubview (commandpane) let Buttonsize:cgfloat= +var commands:dictionary<string, uibutton> =[:] var action:string forNameinch[" Left"," Right"," up"," Down","inch"," out"] {var button= Uibutton.buttonwithtype (Uibuttontype.system) asUIButton button.settitle (name, forState:UIControlState.Normal) Button.settranslatesautoresizin Gmaskintoconstraints (false) Button.addtarget (self, action:selector ("move\ (name)"), forControlEvents:UIControlEvents.TouchUpInside) commands["btn\ (name)"] =button Commandpane.addsubview (button)} var views= ["Commandpane": Commandpane] commandpane.settranslatesautoresizingmaskintoconstraints (false) self.view.addConstraints (Nslayoutconstraint.constraintswithvisualformat ("[Commandpane (260)]", Options:nslayoutformatoptions (0) , Metrics:nil, Views:views) self.view.addConstraints (Nslayoutconstraint.constraintswithvisualformat ("V:|-[commandpane ()", Options:nslayoutformatoptions (0), Metrics:nil, Views:views) self.view.addConstraint (Nslayoutconstraint (Item:commandpane, Attribute:nslayouta Ttribute. CenterX, RelatedBy:NSLayoutRelation.Equal, ToItem:self.view, Attribute:NSLayoutAttribute.CenterX, multiplier:1.0, Constant:0.0)) Let metrics= ["SIZE": +]         for(K,V)inchcommands {v.settranslatesautoresizingmaskintoconstraints (false) commandpane.addconstraints (Nslayoutconstraint.constraintswithvisualformat ("v:|[\ (k) (SIZE)]", Options:nslayoutformatoptions (0) , Metrics:metrics, Views:commands)} commandpane.addconstraints (Nslayoutconstraint.constraintswithvis Ualformat ("h:|[btnleft (size)][btnright (size)][btnup (size)][btndown (size)-(>=0)-[btnout (size)][btnin (size)]|", Options:nslayoutformatoptions (0) }, Metrics:metrics, Views:commands)} func MoveLeft () {Self.moveto (- -,0)} func MoveRight () {Self.moveto ( -,0)} func moveUp () {Self.moveto (0, - -)} func MoveDown () {Self.moveto (0, -)} func moveout () {var rect=Self.viewMoveBlock.frame; Rect.origin.x-= -; RECT.ORIGIN.Y-= -; Rect.size.width+= +; Rect.size.height+= +; Uiview.beginanimations (Nil, Context:nil) uiview.setanimationduration (1.0) Self.viewMoveBlock.frame=rect; Uiview.commitanimations ()} func Movein () {var rect=Self.viewMoveBlock.frame; Rect.origin.x+= -; RECT.ORIGIN.Y+= -; Rect.size.width-= +; Rect.size.height-= +; Uiview.beginanimations (Nil, Context:nil) uiview.setanimationduration (1.0) Self.viewMoveBlock.frame=rect; Uiview.commitanimations ()} func MoveTo (X:cgfloat, _ Y:cgfloat) {var p=Self.viewMoveBlock.center; P.x+=x; P.Y+=y; Uiview.beginanimations (Nil, Context:nil) uiview.setanimationduration (1.0) Self.viewMoveBlock.center=p; Uiview.commitanimations ()}Overridefunc didreceivememorywarning () {super.didreceivememorywarning ()//Dispose of any resources the can be recreated.    }        /*//#pragma mark-navigation//In a storyboard-based application, you'll often want to do a little Preparati On before navigation override func Prepareforsegue (Segue:uistoryboardsegue?, Sender:anyobject?)        {//Get the new view controller using [Segue Destinationviewcontroller].    Pass the selected object to the new view controller. }    */}

4. Running

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.