Masonry for common third-party libraries in iOS
I. preface the layout of Apple has always been a tangled issue. Are you writing code to control the layout or using storyboard to control the layout? In the past, I used code to write constraints in my personal development, because it was too troublesome. Therefore, the final choice is to use AutoLayout for layout, and then drag the line to set constraints. However, many companies use dynamic modification constraints during iOS development, and some use constraints to create some animations, therefore, it is not easy to use storyboard for Development (it is also difficult to use storyboard when several people work together ). However, it seems more efficient to write code and develop code. So many developers started to use Masonry. It is an encapsulated third-party class library to simplify developers' write layout constraints. 2. "Install" Masonry because it is a third-party class library, we can download it from here, decompress it, and drag the Masonry folder into our project folder. 3. Start using Masonry when we use it, we 'd better use AppDelegate. m's-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions; Method to customize loading your own controller. For example, this is what I wrote:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; self.window.backgroundColor = [UIColor whiteColor]; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] init]]; self.window.rootViewController = nav; [self.window makeKeyAndVisible]; return YES;}
Directly load the ViewController written by myself. (We recommend that you delete the Main. stoaryboard that comes with the system. If an error is reported, you can modify it yourself ). The simplest layout without writing Masonry: (the purpose is to add a View to the view)
- (void)viewDidLoad { [super viewDidLoad]; self.title = @"Basic View"; UIView *view1 = [[UIView alloc] init]; view1.translatesAutoresizingMaskIntoConstraints = NO; view1.backgroundColor = [UIColor greenColor]; [superview addSubview:view1]; UIEdgeInsets padding = UIEdgeInsetsMake(74, 10, 10, 10); [superview addConstraints:@[ //view1 constraints [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:padding.top], [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeLeft multiplier:1.0 constant:padding.left], [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-padding.bottom], [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeRight multiplier:1 constant:-padding.right], ]];}