簡體   English   中英

如何在iOS中使用自動布局更改scrollView內容大小

[英]How to change scrollView content sizes using auto-layouts in ios

我在滾動視圖上添加了3個UITextfields和1個UIButton。

我的主要要求是,當我單擊UITextfield時,滾動必須向上滾動到鍵盤上方用戶可見的所有字段。

而且,當我單擊鍵盤上的返回按鈕時,默認情況下必須使用自動布局滾動scrollview contentSize設置的內容。

我的代碼:

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

@end

@implementation ViewController10

- (void)viewDidLoad {
    [super viewDidLoad];

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

    NSDictionary * 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]];
    }

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-30-[emailTextField(30)]-130-[nameTextField(30)]-130-[passwword(30)]-60-[submit(30)]-20-|"
                                                                      options:0
                                                                      metrics:nil
                                                                        views:viewsDic]];

}

-(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];

    return YES;
}

請檢查這個很棒的教程,它絕對對您有幫助。

**

在iOS中將UIScrollView與自動布局一起使用

**

如博客中所述,使用自動布局時,您需要以這種方式設置約束。

 NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeLeading relatedBy:0 toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]; [self.view addConstraint:leftConstraint]; NSLayoutConstraint *rightConstraint = [NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeTrailing relatedBy:0 toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]; [self.view addConstraint:rightConstraint]; 

根據您的要求更改值和名稱。

當出現keyBoards時。

   CGRect frame = currentActiveView.frame; // your scrollview frame 
frame.size.height = SCREEN_HEIGHT - 216 - keyboardAccesssory.frame.size.height;
currentActiveView.frame = frame;

[yourScrollView setContentSize:CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT)];

鍵盤辭職時..

CGRect frame = currentActiveView.frame; // your scrollview frame 
frame.size.height = SCREEN_HEIGHT;

currentActiveView.frame = frame;

[yourScrollView setContentSize:CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT)];

您需要使用視圖的框架。 當顯示鍵盤時,將框移動到鍵盤上方,而在隱藏鍵盤時,將其位置重置。 您需要以編程方式進行。 您也可以閱讀Apple的《編程指南》。 https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html#//apple_ref/doc/uid/TP40009542-CH5-SW3

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM