[英]UIScrollView not showing up on UIViewController
我正在為我的應用程序制作一個簡單的“注冊”屏幕。 我不使用Storyboards
,而是通過代碼來完成所有事情。 因此,為了促進視圖的動態滾動以為鍵盤留出空間,我使用了標准的Apple做法,即將UITextFields
嵌入UIScrollView
,但它們根本沒有顯示!
代碼:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self registerForKeyboardNotifications];
self.title = NSLocalizedString(@"Register", nil);
[self.navigationController setNavigationBarHidden:NO];
// Because Rendering Bug in iOS 7
self.view.backgroundColor = [UIColor whiteColor];
UIScrollView *scrollView = [[UIScrollView alloc] init];
_scrollView.frame = self.view.frame;
_scrollView.scrollEnabled = YES;
_scrollView.showsVerticalScrollIndicator = YES;
_scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height * 2);
[self.view addSubview:scrollView];
// Personal Image View
UIButton *profileImageButton = [UIButton buttonWithType:UIButtonTypeSystem];
profileImageButton.frame = CGRectMake(0, 0, 80, 80);
profileImageButton.center = CGPointMake(self.view.center.x, 80);
[profileImageButton setBackgroundImage:[UIImage imageNamed:@"DefaultAvatar"] forState:UIControlStateNormal];
[profileImageButton addTarget:self action:@selector(uploadImage) forControlEvents:UIControlEventTouchUpInside];
[_scrollView addSubview:profileImageButton];
// Username Text Field
_usernameTextField = [[UITextField alloc] init];
_usernameTextField.frame = CGRectMake(0, 0, self.view.frame.size.width, 64);
_usernameTextField.center = CGPointMake(self.view.center.x, 225);
_usernameTextField.placeholder = @"\tUsername";
_usernameTextField.layer.backgroundColor = [UIColor colorWithRed:244.0f/255.0f green:244.0f/255.0f blue:244.0f/255.0f alpha:1.0].CGColor;
_usernameTextField.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:20.0];
_usernameTextField.autocorrectionType = UITextAutocorrectionTypeNo;
[_scrollView addSubview:_usernameTextField];
// Password Text Field
_passwordTextField = [[UITextField alloc] init];
_passwordTextField.frame = CGRectMake(0, 0, self.view.frame.size.width, 64);
_passwordTextField.center = CGPointMake(self.view.center.x, 320);
_passwordTextField.placeholder = @"\tPassword";
_passwordTextField.layer.backgroundColor = [UIColor colorWithRed:244.0f/255.0f green:244.0f/255.0f blue:244.0f/255.0f alpha:1.0].CGColor;
_passwordTextField.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:20.0];
_passwordTextField.autocorrectionType = UITextAutocorrectionTypeNo;
[_scrollView addSubview:_passwordTextField];
}
以此替換滾動視圖聲明,
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.frame = self.view.frame;
scrollView.scrollEnabled = YES;
scrollView.showsVerticalScrollIndicator = YES;
scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height * 2);
[self.view addSubview:scrollView];
或者像這樣
_scrollView = [[UIScrollView alloc] init];
_scrollView.frame = self.view.frame;
_scrollView.scrollEnabled = YES;
_scrollView.showsVerticalScrollIndicator = YES;
_scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height * 2);
[self.view addSubview:scrollView];
這是因為您初始化了“ scrollView”並添加了“ _scrollView”進行查看。 還要在scrollView中相應地添加所有子視圖。
UIScrollView *scrollView = [[UIScrollView alloc] init];
_scrollView.frame = self.view.frame;
_scrollView.scrollEnabled = YES;
_scrollView.showsVerticalScrollIndicator = YES;
_scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height * 2);
[self.view addSubview:scrollView]; //making mistake here
在這里,您犯錯了,您正在為實例obj分配動態屬性並將本地obj添加為子視圖...
將此行更改為[self.view addSubview:scrollView]; 到[self.view addSubview:_scrollView];
並且做一件事還要分配init _scrollview。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.