2016-12-01 9 views
-1

XcodeでAutoLayoutを使用せずにページ内を上下にスクロールしたいとします。スクロールするにはどうすればいいですか?UIScrollVIewのスクロールダウンObjective C

2番目の質問ラベルとテキストを表示し、他のラベルとフィールドを追加したいとします。しかし、私はプロジェクト

これは、の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]; 

[addTarget self.eyebutton:自己アクション:@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);

がサブビューとして追加する必要がありますすなわち、スクロールビュー上で

そして、あなたは、私は、コードをしようとしている場合ので、あなたのcontentSizeすなわち

self.myScrollView.contentSize = CGSizeMake(self.myScrollView.frame.size.width, sumHeight); 
+0

ようにそれを設定し

CGFloat sumHeight = myLabel1.frame.size.height + myView2.frame.size.height; //etc do for all views you want in your scrollView

つまり、あなたのスクロールビューに含まれるすべてのビューの合計の高さを見つける必要がありますiphone 5でどれくらいのサイズにする必要がありますか?私はちょうどそれを追加する必要があると同様に動作しますか? –

+1

@FaysalSabbagh 'contentSize'は、スクロールビューに入れるコンテンツのサイズです。 – rmaddy

+0

@FaysalSabbaghサイズは、スクロールビューに含まれるすべてのオブジェクトの合計高さ(または水平スクロールが有効かどうかに応じて幅)にする必要があります。はい、あなたのスクロールビューのフレームよりも大きいと仮定します。 –