IOS: Learning notes, adding a controller with interface constraints

Source: Internet
Author: User

1. Create a blank iOS application (empty application).

2. Add the Controller class. Modifying the viewdidload of a controller class

1- (void) Viewdidload2 {3 [Super Viewdidload];4     //Create a title5UILabel *header =[[UILabel alloc] init];6Header.text =@"Welcome to my world!";7Header.textalignment =Nstextalignmentcenter;8 [Self.view Addsubview:header];9     TenSelf.statuslabel =[[UILabel alloc] init]; OneSelf.statusLabel.text =@"Get ready!"; A [Self.view AddSubview:self.statusLabel]; -      -      the     //adding automatic layout constraints -UILabel *statuslabel =Self.statuslabel; - [Header settranslatesautoresizingmaskintoconstraints:no]; - [Statuslabel Settranslatesautoresizingmaskintoconstraints:no]; +      -Nsmutablearray *contraits =[Nsmutablearray array]; +Nsmutabledictionary *metrics = [[Nsmutabledictionary alloc] initwithobjectsandkeys:@5,@"hpadding", @5,@"vpadding", @ -,@"TopMargin", nil]; ANsdictionary *views =nsdictionaryofvariablebindings (header, Statuslabel); at[Contraits addobjectsfromarray:[nslayoutconstraint Constraintswithvisualformat:@"h:|-hpadding-[header]-hpadding-|"Options0Metrics:metrics Views:views]]; -[Contraits addobjectsfromarray:[nslayoutconstraint Constraintswithvisualformat:@"h:|-hpadding-[statuslabel]-hpadding-|"Options0Metrics:metrics Views:views]]; -[Contraits arraybyaddingobjectsfromarray:[nslayoutconstraint Constraintswithvisualformat:@"h:|-hpadding-[statuslabel]-hpadding-|"Options0Metrics:metrics Views:views]]; -[Contraits addobjectsfromarray:[nslayoutconstraint Constraintswithvisualformat:@"v:|-topmargin-[header]-(>=0)-[statuslabel]-vpadding-|"Options0Metrics:metrics Views:views]]; -  - [Self.view addconstraints:contraits]; in      -}

3. Modify the APPDELEGATE.M file

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions{     = [[UIWindow alloc    ] initwithframe:[[uiscreen mainscreen] bounds]; // Override point for customization after application launch.    Self.window.backgroundColor = [Uicolor whitecolor];         // Join the controller    Self.window.rootViewController = [[Demoviewcontroller alloc] Initwithnibname:nil Bundle:nil];        [Self.window makekeyandvisible];     return YES;}

4. Run the program to get the following effect

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.