简体   繁体   中英

How can we reduce code using Autolayouts in ios

Hi i am beginner for auto-layouts and i am inserting "3" textfields and "1" button on scrollview

Here my requirement is according i-phone inches "Top space" must be Adjusting and when i click textfield all fields must be scrolling above keyboard and when i click "return" button in keyboard then scroll must be scrolling as like previous

For this i have tried below code but that is too length code can anybody explain me this concept using short process

my code:-

#import "ViewController10.h"

@interface ViewController10 ()
{
    UIScrollView * scrollView;
    UITextField * emailTextField;
    UITextField * nameTextField;
    UITextField * passwword;
    UIButton * submit;
    NSDictionary * viewsDic;

    NSArray * verticalConstraints;

    int height;
}

@end

@implementation ViewController10

- (void)viewDidLoad {
    [super viewDidLoad];

    height = [UIScreen mainScreen].bounds.size.height;

    scrollView = [[UIScrollView alloc] init];
    scrollView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:scrollView];

    emailTextField = [self createLabelWithText];
    emailTextField.delegate = self;
    [scrollView addSubview: emailTextField];

    nameTextField = [self createLabelWithText];
    nameTextField.delegate = self;
    [scrollView addSubview: nameTextField];

    passwword = [self createLabelWithText];
    passwword.delegate = self;
    [scrollView addSubview: passwword];

    submit = [[UIButton alloc]init];
    submit.backgroundColor = [UIColor orangeColor];
    [submit setTitle: @"Submit" forState: UIControlStateNormal];
    submit.translatesAutoresizingMaskIntoConstraints = NO;
    [scrollView addSubview:submit];

    viewsDic = NSDictionaryOfVariableBindings(scrollView,emailTextField,nameTextField,passwword,submit);

    //Applying autolayouts for scrolview

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-0-[scrollView]-0-|"]
                                                                      options:0
                                                                      metrics:nil
                                                                        views:viewsDic]];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|-0-[scrollView]-0-|"]
                                                                      options:0
                                                                      metrics:nil
                                                                        views:viewsDic]];


    //Applying autolayouts for textfields and button

    [scrollView addConstraint:[NSLayoutConstraint constraintWithItem:emailTextField
                                                           attribute:NSLayoutAttributeCenterX
                                                           relatedBy:NSLayoutRelationEqual
                                                              toItem:scrollView
                                                           attribute:NSLayoutAttributeCenterX
                                                          multiplier:1
                                                            constant:0]];

    NSArray * keys = @[@"emailTextField",@"nameTextField",@"passwword",@"submit"];

    for (NSString * key in keys) {

        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-10-[%@]-10-|",key]
                                                                          options:0
                                                                          metrics:nil
                                                                            views:viewsDic]];
    }

    if (height == 480.0) {

    verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-30-[emailTextField(30)]-130-[nameTextField(30)]-130-[passwword(30)]-60-[submit(30)]-20-|"
                                                                      options:0
                                                                      metrics:nil
                                                                        views:viewsDic];
    }
    else if (height == 568.0){

       verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-50-[emailTextField(30)]-130-[nameTextField(30)]-130-[passwword(30)]-60-[submit(30)]-20-|"
                                                                      options:0
                                                                      metrics:nil
                                                                        views:viewsDic];
    }
    else if(height == 667.0){

      verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-80-[emailTextField(30)]-130-[nameTextField(30)]-130-[passwword(30)]-60-[submit(30)]-20-|"
                                                                      options:0
                                                                      metrics:nil
                                                                        views:viewsDic];
    }

    [scrollView addConstraints:verticalConstraints];
}

-(UITextField *)createLabelWithText{

    UITextField * textfield = [[UITextField alloc] init];
    textfield.textColor = [UIColor whiteColor];
    textfield.backgroundColor = [UIColor lightGrayColor];
    textfield.translatesAutoresizingMaskIntoConstraints = NO;
    return textfield;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{

    scrollView.contentSize = CGSizeMake(320, 700);
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    [textField resignFirstResponder];

    [scrollView removeConstraints:verticalConstraints];

    if (height == 480.0) {

        verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-30-[emailTextField(30)]-130-[nameTextField(30)]-130-[passwword(30)]-60-[submit(30)]-20-|"
                                                                      options:0
                                                                      metrics:nil
                                                                        views:viewsDic];
    }

    else if (height == 568.0){

        verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-50-[emailTextField(30)]-130-[nameTextField(30)]-130-[passwword(30)]-60-[submit(30)]-20-|"
                                                                      options:0
                                                                      metrics:nil
                                                                        views:viewsDic];
    }

    else if(height == 667.0){

        verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-80-[emailTextField(30)]-130-[nameTextField(30)]-130-[passwword(30)]-60-[submit(30)]-20-|"
                                                                      options:0
                                                                      metrics:nil
                                                                        views:viewsDic];
    }

    [scrollView addConstraints:verticalConstraints];

    return YES;
}

@end

尝试砌体,它可以减少代码,并且您更容易维护和理解https://github.com/SnapKit/Masonry

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM