UIScrollView 未显示在 UIViewController 上

Posted

技术标签:

【中文标题】UIScrollView 未显示在 UIViewController 上【英文标题】:UIScrollView not showing up on UIViewController 【发布时间】: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];

【问题讨论】:

【参考方案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];

或者像这样

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

【讨论】:

【参考方案2】:
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。

【讨论】:

以上是关于UIScrollView 未显示在 UIViewController 上的主要内容,如果未能解决你的问题,请参考以下文章

UIView 未显示在 UIScrollView 中

当前图像未使用 UIScrollview 显示在照片库中

为啥未显示集合可重用视图?

目标 - C UIScrollView 不能水平工作

iOS— UIScrollView和 UIPageControl之间的那些事

记录 UIScrollView 的子视图会显示未添加的子视图