添加滚动视图时,所有视图都显示在导航栏后面的self.view顶部

Posted

技术标签:

【中文标题】添加滚动视图时,所有视图都显示在导航栏后面的self.view顶部【英文标题】:All the views are displayed on the top of the self.view behind the navigation bar when adding scroll view 【发布时间】:2017-02-14 15:16:26 【问题描述】:

我是 ios 新手。我最近尝试了滚动视图。但是滚动视图的所有子视图都显示在导航栏的后面。我在 3d 视图中检查但放置了一个断点。我从 viewDidLoad 调用这个方法。帮助

#import "AddContactViewController.h"
#import "NSStringCategory.h"
#import "ContactList.h"
#import "AddContactDelegate.h"
#import "ContactsListViewController.h"
#import "ContactDisplayingViewController.h"
#import "Contact.h"
@interface AddContactViewController ()
@property NSMutableArray * textFields;
@property UIDatePicker *inputViewDatePicker;
@property UIDatePicker *inputViewDatePicker1;
@property UIScrollView * scrollView;
@end

@implementation AddContactViewController
#pragma mark - Setting up the view
-(void)createView
    /*CGFloat x = 0;
    CGFloat y = self.navigationController.navigationBar.frame.size.height;
    CGFloat widthCG = self.view.frame.size.width;
    CGFloat heightCG = self.view.frame.size.height;*/
    NSLayoutConstraint * myConstraint;
    //Date pickers
    self.inputViewDatePicker = [[UIDatePicker alloc]init];
    self.inputViewDatePicker.datePickerMode = UIDatePickerModeDate;
    self.inputViewDatePicker1 = [[UIDatePicker alloc]init];
    self.inputViewDatePicker1.datePickerMode = UIDatePickerModeDate;
    [self.inputViewDatePicker addTarget:self action:@selector(datePickerChanged:) forControlEvents:UIControlEventValueChanged];
    [self.inputViewDatePicker1 addTarget:self action:@selector(expiryDatePickerChanged:) forControlEvents:UIControlEventValueChanged];
    self.scrollView = [[UIScrollView alloc]init];
    //self.scrollView.frame = self.view.frame;
//    self.scrollView.frame = CGRectMake(0, 0, 500, 500);
    NSLog(@"The frame of the view is %@",self.view.frame);
//    self.scrollView.contentSize = CGSizeMake(414,736);
    [self.view addSubview:self.scrollView];
    self.scrollView.translatesAutoresizingMaskIntoConstraints = NO;

    myConstraint = [NSLayoutConstraint constraintWithItem:self.scrollView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1 constant:0];
    [self.view addConstraint:myConstraint];

    myConstraint = [NSLayoutConstraint constraintWithItem:self.scrollView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1 constant:0];
    [self.view addConstraint:myConstraint];

    myConstraint = [NSLayoutConstraint constraintWithItem:self.scrollView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:0];
    [self.view addConstraint:myConstraint];

    myConstraint = [NSLayoutConstraint constraintWithItem:self.scrollView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0];
    [self.view addConstraint:myConstraint];
    UILabel *name = [[UILabel alloc]init];
    [name setText:@"Name"];
    name.translatesAutoresizingMaskIntoConstraints = NO;

    [self.scrollView addSubview:name];
    self.nameTextField = [[UITextField alloc]init];
    self.nameTextField.placeholder = @"Name";
    myConstraint =[NSLayoutConstraint constraintWithItem:name
                                               attribute:NSLayoutAttributeBottom
                                               relatedBy:NSLayoutRelationEqual
                                                  toItem:self.scrollView
                                               attribute:NSLayoutAttributeBaseline
                                              multiplier:((float)2/10)
                                                constant:0];
    [self.scrollView addConstraint:myConstraint];
    myConstraint = [NSLayoutConstraint constraintWithItem:name attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeLeft multiplier:1 constant:10];
    [self.scrollView addConstraint:myConstraint];

    self.nameTextField.translatesAutoresizingMaskIntoConstraints = NO;
    self.nameTextField.borderStyle = UITextBorderStyleLine;
    NSLog(@"%@",[self.nameTextField class]);
    [self.scrollView addSubview:self.nameTextField];
    myConstraint =[NSLayoutConstraint constraintWithItem:self.nameTextField
                                               attribute:NSLayoutAttributeBottom
                                               relatedBy:NSLayoutRelationEqual
                                                  toItem:self.scrollView
                                               attribute:NSLayoutAttributeBaseline
                                              multiplier:((float)2/10)
                                                constant:0];
    [self.scrollView addConstraint:myConstraint];

    myConstraint = [NSLayoutConstraint constraintWithItem:self.nameTextField
                                                attribute:NSLayoutAttributeRight
                                                relatedBy:NSLayoutRelationEqual
                                                   toItem:self.scrollView
                                                attribute:NSLayoutAttributeRight
                                               multiplier:1
                                                 constant:-10];
    [self.scrollView addConstraint:myConstraint];
    myConstraint = [NSLayoutConstraint constraintWithItem:self.nameTextField
                                                attribute:NSLayoutAttributeWidth
                                                relatedBy:NSLayoutRelationEqual
                                                   toItem:self.scrollView
                                                attribute:NSLayoutAttributeWidth
                                               multiplier:((float)0.65)
                                                 constant:0];
    [self.scrollView addConstraint:myConstraint];


    //Second Name
    UILabel *secondName = [[UILabel alloc]init];
    [secondName setText:@"Second Name"];
    [secondName sizeToFit];
    [self.scrollView addSubview:secondName];
    self.secondNameTextField = [[UITextField alloc]init];
    self.secondNameTextField.placeholder = @"Second Name";
    myConstraint =[NSLayoutConstraint constraintWithItem:secondName
                                               attribute:NSLayoutAttributeBottom
                                               relatedBy:NSLayoutRelationEqual
                                                  toItem:self.scrollView
                                               attribute:NSLayoutAttributeBaseline
                                              multiplier:((float)3.5/10)
                                                constant:0];
    secondName.translatesAutoresizingMaskIntoConstraints = NO;
    [self.scrollView addConstraint:myConstraint];

    myConstraint = [NSLayoutConstraint constraintWithItem:secondName attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeLeft multiplier:1 constant:10];
    myConstraint =[NSLayoutConstraint constraintWithItem:self.secondNameTextField
                                               attribute:NSLayoutAttributeBottom
                                               relatedBy:NSLayoutRelationEqual
                                                  toItem:self.scrollView
                                               attribute:NSLayoutAttributeBaseline
                                              multiplier:((float)3.5/10)
                                                constant:0];
    [self.scrollView addSubview:self.secondNameTextField];
    [self.scrollView addConstraint:myConstraint];
    self.secondNameTextField.translatesAutoresizingMaskIntoConstraints = NO;
    self.secondNameTextField.borderStyle = UITextBorderStyleLine;
    myConstraint = [NSLayoutConstraint constraintWithItem:self.secondNameTextField
                                                attribute:NSLayoutAttributeRight
                                                relatedBy:NSLayoutRelationEqual
                                                   toItem:self.scrollView
                                                attribute:NSLayoutAttributeRight
                                               multiplier:1
                                                 constant:-10];
    [self.scrollView addConstraint:myConstraint];
    myConstraint = [NSLayoutConstraint constraintWithItem:self.secondNameTextField
                                                attribute:NSLayoutAttributeWidth
                                                relatedBy:NSLayoutRelationEqual
                                                   toItem:self.scrollView
                                                attribute:NSLayoutAttributeWidth
                                               multiplier:((float)0.65)
                                                 constant:0];
    [self.scrollView addConstraint:myConstraint];


    //Email
    self.emailTextField = [[UITextField alloc]init];
    UILabel *email = [[UILabel alloc]init];
    [email setText:@"Email"];
    [self.scrollView addSubview:email];
    myConstraint =[NSLayoutConstraint constraintWithItem:email
                                               attribute:NSLayoutAttributeBottom
                                               relatedBy:NSLayoutRelationEqual
                                                  toItem:[email superview]
                                               attribute:NSLayoutAttributeBaseline
                                              multiplier:((float)5/10)
                                                constant:0];
    email.translatesAutoresizingMaskIntoConstraints = NO;
    self.emailTextField.translatesAutoresizingMaskIntoConstraints = NO;
    self.emailTextField.borderStyle = UITextBorderStyleLine;
    self.emailTextField.placeholder = @"Email";

    [self.scrollView addConstraint:myConstraint];
    myConstraint = [NSLayoutConstraint constraintWithItem:email attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeLeft multiplier:1 constant:10];
    [self.scrollView addConstraint:myConstraint];
    [self.scrollView addSubview:self.emailTextField];

    myConstraint =[NSLayoutConstraint constraintWithItem:self.emailTextField
                                               attribute:NSLayoutAttributeBottom
                                               relatedBy:NSLayoutRelationEqual
                                                  toItem:self.scrollView
                                               attribute:NSLayoutAttributeBaseline
                                              multiplier:((float)5/10)
                                                constant:0];
    [self.scrollView addConstraint:myConstraint];
    myConstraint = [NSLayoutConstraint constraintWithItem:self.emailTextField attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeRight multiplier:1 constant:-10];
    [self.scrollView addConstraint:myConstraint];
    myConstraint = [NSLayoutConstraint constraintWithItem:self.emailTextField attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeWidth multiplier:((float)0.65) constant:0];
    [self.scrollView addConstraint:myConstraint];


    //Number
    UILabel * number = [[UILabel alloc]init];
    number.translatesAutoresizingMaskIntoConstraints = NO;
    [number setText:@"Number"];
    [self.scrollView addSubview:number];
    myConstraint =[NSLayoutConstraint constraintWithItem:number
                                               attribute:NSLayoutAttributeBottom
                                               relatedBy:NSLayoutRelationEqual
                                                  toItem:[number superview]
                                               attribute:NSLayoutAttributeBaseline
                                              multiplier:((float)6.5/10)
                                                constant:0];
    self.numberTextField.translatesAutoresizingMaskIntoConstraints = NO;
    [self.scrollView addConstraint:myConstraint];
    myConstraint = [NSLayoutConstraint constraintWithItem:number attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeLeft multiplier:1 constant:10];
    [self.scrollView addConstraint:myConstraint];
    self.numberTextField = [[UITextField alloc]init];
    self.numberTextField.placeholder = @"Number";
    self.numberTextField.translatesAutoresizingMaskIntoConstraints = NO;
    self.numberTextField.borderStyle = UITextBorderStyleLine;
    myConstraint =[NSLayoutConstraint constraintWithItem:self.numberTextField
                                               attribute:NSLayoutAttributeBottom
                                               relatedBy:NSLayoutRelationEqual
                                                  toItem:self.scrollView
                                               attribute:NSLayoutAttributeBaseline
                                              multiplier:((float)6.5/10)
                                                constant:0];
    [self.scrollView addSubview:self.numberTextField];
    [self.scrollView addConstraint:myConstraint];
    myConstraint = [NSLayoutConstraint constraintWithItem:self.numberTextField attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeRight multiplier:1 constant:-10];
    [self.scrollView addConstraint:myConstraint];
    myConstraint = [NSLayoutConstraint constraintWithItem:self.numberTextField attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeWidth multiplier:((float)0.65) constant:0];
    [self.scrollView addConstraint:myConstraint];


    //Date of Birth
    NSDate *now = [[NSDate alloc]init];
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY:MM:dd"];
    self.theDob = [[UITextField alloc]init];
    self.theDob.translatesAutoresizingMaskIntoConstraints = NO;
    self.theDob.borderStyle = UITextBorderStyleLine;
    //[self.theDob addTarget:self action:@selector(DobTapped:) forControlEvents:UIControlEventAllTouchEvents];
    self.theDob.userInteractionEnabled = true;
    self.theDob.enabled = true;
    self.theDob.inputView = self.inputViewDatePicker;
    self.theDob.text = [dateFormatter stringFromDate:now];
    NSLog(@"The date is %@",[dateFormatter stringFromDate:now]);
    [self.scrollView addSubview: self.theDob];
    myConstraint =[NSLayoutConstraint constraintWithItem:self.theDob
                                               attribute:NSLayoutAttributeBottom
                                               relatedBy:NSLayoutRelationEqual
                                                  toItem:self.scrollView
                                               attribute:NSLayoutAttributeBaseline
                                              multiplier:((float)8/10)
                                                constant:0];
    [self.scrollView addConstraint:myConstraint];
    myConstraint = [NSLayoutConstraint constraintWithItem:self.theDob attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeRight multiplier:1 constant:-10];
    [self.scrollView addConstraint:myConstraint];
    myConstraint = [NSLayoutConstraint constraintWithItem:self.theDob attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeWidth multiplier:((float)0.65) constant:0];
    [self.scrollView addConstraint:myConstraint];
    UILabel *dob = [[UILabel alloc]init];
    [dob setText:@"DOB"];
    [self.scrollView addSubview:dob];
    myConstraint =[NSLayoutConstraint constraintWithItem:dob
                                               attribute:NSLayoutAttributeBottom
                                               relatedBy:NSLayoutRelationEqual
                                                  toItem:[dob superview]
                                               attribute:NSLayoutAttributeBaseline
                                              multiplier:((float)8/10)
                                                constant:0];
    dob.translatesAutoresizingMaskIntoConstraints = NO;
    [self.scrollView addConstraint:myConstraint];
    myConstraint = [NSLayoutConstraint constraintWithItem:dob attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeLeft multiplier:1 constant:10];
    [self.scrollView addConstraint:myConstraint];


    //Expiry Date
    self.expirydate = [[UITextField alloc]init];
    self.expirydate.translatesAutoresizingMaskIntoConstraints = NO;
    [self.scrollView addSubview: self.expirydate];
    myConstraint =[NSLayoutConstraint constraintWithItem:self.expirydate
                                               attribute:NSLayoutAttributeBottom
                                               relatedBy:NSLayoutRelationEqual
                                                  toItem:self.scrollView
                                               attribute:NSLayoutAttributeBaseline
                                              multiplier:((float)9.5/10)
                                                constant:0];
    [self.scrollView addConstraint:myConstraint];
    myConstraint = [NSLayoutConstraint constraintWithItem:self.expirydate attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeRight multiplier:1 constant:-10];
    [self.scrollView addConstraint:myConstraint];
    myConstraint = [NSLayoutConstraint constraintWithItem:self.expirydate attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeWidth multiplier:((float)0.65) constant:0];
    self.expirydate.text = [dateFormatter stringFromDate:now];
    self.expirydate.userInteractionEnabled = true;
    self.expirydate.enabled = true;
    self.expirydate.borderStyle = UITextBorderStyleLine;
    self.expirydate.text = [dateFormatter stringFromDate:[[NSDate alloc]init] ];
    self.expirydate.inputView = self.inputViewDatePicker1;
    [self.scrollView addConstraint:myConstraint];
    UILabel *expiryDate = [[UILabel alloc]init];
    [expiryDate setText:@"Expiry Date"];
    [expiryDate sizeToFit];
    [self.scrollView addSubview:expiryDate];
    NSLog(@"%@",NSStringFromCGRect(self.scrollView.frame));
    NSLog(@"%@",NSStringFromCGRect(self.nameTextField.frame));
    NSLog(@"%@ super class",[self.scrollView superview]);
    NSLog(@"the scroll super class %@",[self.nameTextField superview]);
    myConstraint =[NSLayoutConstraint constraintWithItem:expiryDate
                                               attribute:NSLayoutAttributeBottom
                                               relatedBy:NSLayoutRelationEqual
                                                  toItem:self.scrollView
                                               attribute:NSLayoutAttributeBaseline
                                              multiplier:((float)9.5/10)
                                                constant:0];
    expiryDate.translatesAutoresizingMaskIntoConstraints = NO;
    [self.scrollView addConstraint:myConstraint];
    myConstraint = [NSLayoutConstraint constraintWithItem:expiryDate attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeLeft multiplier:1 constant:10];
    [self.scrollView addConstraint:myConstraint];
    //[self.scrollView addSubview:self.scrollView];

    //Navigation Buttons
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Save" style:UIBarButtonItemStylePlain target:self action:@selector(saveContact:)];

    myConstraint = [NSLayoutConstraint constraintWithItem:self.nameTextField attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:name attribute:NSLayoutAttributeRight multiplier:1 constant:20];
    [self.scrollView addConstraint:myConstraint];
    myConstraint = [NSLayoutConstraint constraintWithItem:self.emailTextField attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:email attribute:NSLayoutAttributeRight multiplier:1 constant:20];
    [self.scrollView addConstraint:myConstraint];
    myConstraint = [NSLayoutConstraint constraintWithItem:self.secondNameTextField attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:secondName attribute:NSLayoutAttributeRight multiplier:1 constant:20];
    [self.scrollView addConstraint:myConstraint];
    myConstraint = [NSLayoutConstraint constraintWithItem:self.numberTextField attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:number attribute:NSLayoutAttributeRight multiplier:1 constant:20];
    [self.scrollView addConstraint:myConstraint];
    myConstraint = [NSLayoutConstraint constraintWithItem:self.theDob attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:dob attribute:NSLayoutAttributeRight multiplier:1 constant:20];
    [self.scrollView addConstraint:myConstraint];
    myConstraint = [NSLayoutConstraint constraintWithItem:self.expirydate attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:expiryDate attribute:NSLayoutAttributeRight multiplier:1 constant:20];
    [self.scrollView addConstraint:myConstraint];

【问题讨论】:

没有。它没有帮助。我的问题是我猜滚动视图的高度不是设置。所有视图都出现在 0 yth 位置。 【参考方案1】:

您的问题的一部分 - 您正在设置相对于滚动视图的 NSLayoutAttributeBaseline 的对象的约束...我相信它始终为 0。

我建议您尝试让第一个标签显示在您想要的位置。之后,一次添加一个元素,使用约束将它们相对于第一个标签定位。

试试这个作为你的第一个约束:

myConstraint =[NSLayoutConstraint constraintWithItem:name
                                           attribute:NSLayoutAttributeBaseline
                                           relatedBy:NSLayoutRelationEqual
                                              toItem:self.scrollView
                                           attribute:NSLayoutAttributeTop
                                          multiplier:1
                                            constant:20];

应该将“姓名”标签的基线放置在距离滚动视图顶部 20 点的位置。

【讨论】:

【参考方案2】:

你可以试试下面的

[self.navigationController.navigationBar setTranslucent:NO];

这样,您的self.view 的所有子视图都不会位于UINavigationBar 后面。

如果它没有帮助或者您正在寻找其他东西,请告诉我。

【讨论】:

没有。实际上我希望视图填充滚动视图。现在它们只出现在顶部。在 0 y 位置

以上是关于添加滚动视图时,所有视图都显示在导航栏后面的self.view顶部的主要内容,如果未能解决你的问题,请参考以下文章

Web视图添加hardwareAccelerated时的滚动问题为false

iOS:滚动表格视图时隐藏和显示自定义导航栏

图像视图在滚动视图中隐藏导航栏

如何将视图添加到整个屏幕

按下导航栏中的后退按钮并弹出视图B后更新视图B后面的视图A.

隐藏 NavBar 但显示带背景的状态栏