【发布时间】:2014-02-25 04:28:05
【问题描述】:
我正在为我的应用制作一个简单的注册屏幕。我没有使用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];
}
【问题讨论】:
标签: ios iphone objective-c uiviewcontroller uiscrollview