我是ios的新手。我最近尝试过scrollview。但滚动视图的所有子视图都显示在导航栏后面。我在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 :(得分:0)
您可以尝试以下
[self.navigationController.navigationBar setTranslucent:NO];
这样,self.view
的子视图都不会位于UINavigationBar
后面。
如果它没有帮助或者你正在寻找其他东西,请告诉我。
答案 1 :(得分:0)
你的问题的一部分 - 你正在设置相对于滚动视图的NSLayoutAttributeBaseline
的对象的约束...我相信,这总是为0。
我建议你尝试让第一个标签显示在你想要的位置。之后,一次添加一个元素,使用约束将它们相对于第一个标签定位。
试试这个第一个约束:
myConstraint =[NSLayoutConstraint constraintWithItem:name
attribute:NSLayoutAttributeBaseline
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeTop
multiplier:1
constant:20];
应该将“名称”标签的基线放在距离滚动视图顶部20点的位置。