2016-12-01 50 views
-1

我想在頁面中上下滾動而不在Xcode中使用AutoLayout。我如何進行滾動工作?在UIScrollVIew中向下滾動目標C

我想顯示第二個問題標籤和文本,並添加其他標籤和字段。但我不能滾動查看建設項目

這是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:自我行動:@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]; 

}

回答

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); 
+0

所以如果我想要的代碼在iphone 5上應該有多大的尺寸?以及我只是需要添加它,它會工作? –

+1

@FaysalSabbagh'contentSize'是你放入滾動視圖的內容的大小。 – rmaddy

+0

@FaysalSabbagh大小應該是包含在滾動視圖中的所有對象的總高度(或寬度取決於水平滾動是否啓用)。是的,假設總和大於你的滾動視圖的框架 –