我想在我的页面中向上和向下滚动而不使用Xcode中的AutoLayout。如何使滚动工作?
我想显示第二个问题标签和文字,并添加其他标签和字段。但我无法滚动查看他们在构建项目后的看法
这是viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImage *bg = [UIImage imageNamed:@"logo2.jpg"];
self.myImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
self.myImageView.image = bg;
self.myImageView.contentMode = UIViewContentModeScaleAspectFit;
self.myImageView.center = self.view.center;
self.view.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.myImageView];
CGRect scrollViewRect = self.view.bounds;
self.myScrollView = [[UIScrollView alloc] initWithFrame:scrollViewRect];
self.myScrollView.pagingEnabled = YES;
self.myScrollView.scrollEnabled = YES;
self.myScrollView.contentSize = CGSizeMake(self.myScrollView.frame.size.width + 100, self.myScrollView.frame.size.height + 100);
[self.view addSubview:self.myScrollView];
float width = CGRectGetWidth(self.myScrollView.frame);
float height = CGRectGetHeight(self.myScrollView.frame);
float newPosition = self.myScrollView.contentOffset.y+height;
CGRect toVisible = CGRectMake(newPosition, 0, width, height);
[self.myScrollView scrollRectToVisible:toVisible animated:YES];
//FirstNameLabel
CGRect labelFirst = CGRectMake(10, 100, 100, 30);
self.firstnamelabel = [[UILabel alloc] initWithFrame:labelFirst];
self.firstnamelabel.font = [UIFont boldSystemFontOfSize:15.0f];
self.firstnamelabel.text = @"First Name : ";
//FirstNameField
CGRect firsttextField = CGRectMake(100, 100, 200, 30);
self.firstnametext = [[UITextField alloc] initWithFrame:firsttextField];
self.firstnametext.borderStyle = UITextBorderStyleRoundedRect;
//LastNameLabel
CGRect labelLast = CGRectMake(10, 150, 100, 30);
self.lastnamelabel = [[UILabel alloc] initWithFrame:labelLast];
self.lastnamelabel.font = [UIFont boldSystemFontOfSize:15.0f];
self.lastnamelabel.text = @"Last Name : ";
//LastNameTextField
CGRect lasttextField = CGRectMake(100, 150, 200, 30);
self.lastnametext = [[UITextField alloc] initWithFrame:lasttextField];
self.lastnametext.borderStyle = UITextBorderStyleRoundedRect;
//DateLabel
CGRect labeldate = CGRectMake(10, 190, 100, 30);
self.DateOfBirthLabel = [[UILabel alloc] initWithFrame:labeldate];
self.DateOfBirthLabel.font = [UIFont boldSystemFontOfSize:15.0f];
self.DateOfBirthLabel.text = @"Date Of Birth: ";
//DatePicker
CGRect datepickersize=CGRectMake(10, 210, 300, 150);
self.datepicker=[[UIDatePicker alloc] initWithFrame:datepickersize];
NSDate *date= self.datepicker.date;
self.datepicker.datePickerMode=UIDatePickerModeDate;
//Email
CGRect labelemail = CGRectMake(30, 370, 100, 30);
self.emaillabel = [[UILabel alloc] initWithFrame:labelemail];
self.emaillabel.font = [UIFont boldSystemFontOfSize:15.0f];
self.emaillabel.text = @"Email : ";
//EmailText
CGRect emailtextField = CGRectMake(100, 370, 200, 30);
self.emailtext = [[UITextField alloc] initWithFrame:emailtextField];
self.emailtext.borderStyle = UITextBorderStyleRoundedRect;
//PasswordLabel
CGRect labelpassword = CGRectMake(10, 410, 100, 30);
self.passwordlabel = [[UILabel alloc] initWithFrame:labelpassword];
self.passwordlabel.font = [UIFont boldSystemFontOfSize:15.0f];
self.passwordlabel.text = @"Password : ";
//PasswordText
CGRect passwordtextField = CGRectMake(100, 410, 180, 30);
self.passwordtext = [[UITextField alloc] initWithFrame:passwordtextField];
self.passwordtext.borderStyle = UITextBorderStyleRoundedRect;
self.passwordtext.secureTextEntry = true;
//eyebutton
UIImage *eye = [UIImage imageNamed:@"eye.png"];
self.eyebutton = [UIButton buttonWithType:UIButtonTypeCustom];
self.eyebutton.frame = CGRectMake(290, 415, 20, 20);
[self.eyebutton setImage:eye forState:UIControlStateNormal];
[self.eyebutton addTarget:self action:@selector(touchDown) forControlEvents:UIControlEventTouchDown];
[self.eyebutton addTarget:self action:@selector(touchUpInside)forControlEvents:UIControlEventTouchUpInside];
//FirstQuetionLabel
CGRect labelfq = CGRectMake(10, 450, 300, 20);
self.firstQuestlabel = [[UILabel alloc] initWithFrame:labelfq];
self.firstQuestlabel.font = [UIFont boldSystemFontOfSize:14.0f];
self.firstQuestlabel.text = @"Question 1: In which city you were born in?";
//FirstQuestionTextField
CGRect fqtextField = CGRectMake(10, 470, 240, 30);
self.firstQuestionText = [[UITextField alloc] initWithFrame:fqtextField];
self.firstQuestionText.borderStyle = UITextBorderStyleRoundedRect;
//SecondQuestionLabel
CGRect labelsq = CGRectMake(10, 500, 300, 30);
self.secondQuestLabel = [[UILabel alloc] initWithFrame:labelsq];
self.secondQuestLabel.font = [UIFont boldSystemFontOfSize:13.0f];
self.secondQuestLabel.text = @"Question 2: What is your mother's \rmiddle name?";
//SecondQuestionTextField
CGRect sqtextField = CGRectMake(10, 630, 240, 30);
self.secondQuestionText = [[UITextField alloc] initWithFrame:sqtextField];
self.secondQuestionText.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:self.eyebutton];
[self.view addSubview:self.firstnamelabel];
[self.view addSubview:self.firstnametext];
[self.view addSubview:self.lastnamelabel];
[self.view addSubview:self.lastnametext];
[self.view addSubview:self.DateOfBirthLabel];
[self.view addSubview:self.datepicker];
[self.view addSubview:self.emaillabel];
[self.view addSubview:self.emailtext];
[self.view addSubview:self.passwordlabel];
[self.view addSubview:self.passwordtext];
[self.view addSubview:self.firstQuestlabel];
[self.view addSubview:self.firstQuestionText];
[self.view addSubview:self.secondQuestLabel];
[self.view addSubview:self.secondQuestionText];
}
答案 0 :(得分:2)
乍一看,由于您尚未设置滚动视图的contentSize属性,因此可能无法启用滚动。要设置允许滚动,需要将contentSize设置为大于滚动视图的框架。
objective-c中的一个例子是:
self.myScrollView.contentSize = CGSizeMake(self.myScrollView.frame.size.width + 20, self.myScrollView.frame.size.height + 20);
针对已修改的问题进行了更新
您想要在滚动视图中滚动的任何视图都需要作为子视图添加到scrollView上,即
[self.myScrollView addSubview: myLabel]; //do for all views that you want to be scrollable in the scrollView
然后你需要找到滚动视图中包含的所有视图的总和高度,即
CGFloat sumHeight = myLabel1.frame.size.height + myView2.frame.size.height; //etc do for all views you want in your scrollView
然后将其设置为您的contentSize即
self.myScrollView.contentSize = CGSizeMake(self.myScrollView.frame.size.width, sumHeight);