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: