IOS: learning notes, adding a controller with interface Constraints

Source: Internet
Author: User

1. Create an Empty iOS Application ).

2. Add the Controller class. Modify the viewDidLoad of the controller class.

-(Void) viewDidLoad {[super viewDidLoad]; // create the title UILabel * header = [[UILabel alloc] init]; header. text = @ "welcome to my world! "; Header. textAlignment = NSTextAlignmentCenter; [self. view addSubview: header]; self. statusLabel = [[UILabel alloc] init]; self. statusLabel. text = @" ready! "; [Self. view addSubview: self. statusLabel]; // Add the Automatic Layout constraint UILabel * statusLabel = self. statusLabel; [header failed: NO]; [statusLabel failed: NO]; NSMutableArray * contraits = [NSMutableArray array]; NSMutableDictionary * metrics = [[NSMutableDictionary alloc] Comment: @ 5, @ "HPadding", @ 5, @ "VPadding", @ 20, @ "TopMargin", nil]; NSDictionary * views = Authorization (header, statusLabel); [contraits addObjectsFromArray: [NSLayoutConstraint failed: @ "H: |-HPadding-[header]-HPadding-|" options: 0 metrics: metrics views: views]; [contraits failed: [NSLayoutConstraint constraintsWithVisualFormat: @ "H: |-HPadding-[statusLabel]-HPadding-|" options: 0 metrics: metrics views: views]; [contraits usage: [NSLayoutConstraint constraintsWithVisualFormat: @ "H: |-HPadding-[statusLabel]-HPadding-| "options: 0 metrics: metrics views: views]; [contraits addObjectsFromArray: [NSLayoutConstraint constraintsWithVisualFormat: @" V: |-TopMargin-[header]-(> = 0)-[statusLabel]-VPadding-| "options: 0 metrics: metrics views: views]; [self. view addConstraints: contraits];}

3. Modify the AppDelegate. m file

-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {self. window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]; // Override point for customization after application launch. self. window. backgroundColor = [UIColor whiteColor]; // Add the controller self. window. rootViewController = [[DemoViewController alloc] initWithNibName: nil bundle: nil]; [self. window makeKeyAndVisible]; return YES ;}

4. Run the program and get the following results:

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.