UIScrollView没有显示在UIViewController上

时间:2014-02-25 04:28:06

标签: ios iphone objective-c uiviewcontroller uiscrollview

我正在为我的应用制作一个简单的注册屏幕。我没有使用Storyboards并且正在通过代码执行所有操作。因此,为了方便动态滚动我的视图以便为键盘留出空间我正在使用标准的Apple将UITextFields嵌入到UIScrollView中的做法,但它们根本没有出现! / p>

代码:

- (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];
}

2 个答案:

答案 0 :(得分:2)

将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];

或者像这样

 _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中添加所有子视图。

答案 1 :(得分:1)

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];                  to [self.view addSubview:_scrollView];

并做一件事也分配init _scrollview。