Masonry for common third-party libraries in iOS

Source: Internet
Author: User

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],                                                                ]];}

 

 

Related Article

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.