为什么添加对象后 UIScrollView 不会完全滚动?使用故事板、ARC 和 Xcode 4.5.2 [英] Why won't UIScrollView scroll fully after adding objects? Using storyboard, ARC, and Xcode 4.5.2

查看:16
本文介绍了为什么添加对象后 UIScrollView 不会完全滚动?使用故事板、ARC 和 Xcode 4.5.2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我知道我有类似的问题,但可能不准确(所以请不要标记我——只是警告我或其他什么).我已经搜索了几天来解决这个简单的问题.使用故事板、ARC 和 Xcode 4.5.2,我只需将一堆标签放在 UIScrollView 中并让它垂直滚动.我已经尝试过在 viewDidLoad、viewDidAppear 和 viewWillAppear 中设置帧大小和内容大小的多种组合,但无济于事.当里面没有任何东西时,滚动视图可以完美滚动,但是当我给它添加标签时,滚动只会滚动很短的部分.

So, I know there are similar questions to mine, but maybe not exact (so please don't mark me down -- just warn me or something). I have searched for days for the solution to this SIMPLE issue. Using storyboards, ARC, and Xcode 4.5.2, I simply need to put a bunch of labels inside a UIScrollView and have it scroll vertically. I've tried so many combinations of setting frame sizes and content sizes within viewDidLoad, viewDidAppear, and viewWillAppear, but to no avail. The scroll view scrolls perfectly when there's nothing inside of it, but when I add labels to it, the scrolling only scrolls a very short section.

注意:我需要使用自动布局,否则我的整个项目都会搞砸.

Note: I need to use auto layout, otherwise my whole project will get messed up.

这是我当前的代码...

Here's my current code...

.h 文件:

#import <UIKit/UIKit.h>

@interface MortgageRatesViewController : UIViewController <UIScrollViewDelegate, UIScrollViewAccessibilityDelegate>

- (IBAction)backButton:(id)sender;

@property (strong, nonatomic) IBOutlet UIView *mortgageView;

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@end

.m 文件:

#import "MortgageRatesViewController.h"


@interface MortgageRatesViewController ()

@end

@implementation MortgageRatesViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

//-------------------------------------------------------

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"appBackgroundColor.png"]];


    [self.scrollView setScrollEnabled:YES];
    [self.scrollView setContentSize:CGSizeMake(0, 809)];



}

//---------------------------------------------------------------

//---------------------------------------------------------------

//-(void)viewWillAppear:(BOOL)animated{
//    
//    
//    [super viewWillAppear:animated];
//    
//    
//    [self.scrollView setFrame:CGRectMake(0, 0, 320, 808)];   
//
//    
//}

//---------------------------------------------------------------

-(void)viewDidAppear:(BOOL)animated
{

    [super viewDidAppear:animated];

    [self.view addSubview:self.scrollView];
    [self.scrollView setScrollEnabled:YES];
    [self.scrollView setContentSize:CGSizeMake(0, 809)];

}

//-------------------------------------------------------------

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)backButton:(id)sender {

    [self dismissViewControllerAnimated:YES completion:NO];

}
@end

注意:将 viewWillAppear 注释掉没有任何区别.

我在下面发布了解决方案.希望它可以帮助其他人!

推荐答案

经过几天的研究,有趣的是,我居然在发布这个问题几分钟后就找到了解决方案!所以,对于我的情况,我只需要添加这段代码,它就起作用了:

After days of research, it's funny I actually found the solution just minutes after posting this question! So, for my situation I simply had to add this bit of code, and it worked:

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    [self.scrollView setContentSize:CGSizeMake(320, 808)];
}

它现在似乎运行良好.任何人,请让我知道这是否是一种糟糕的做法,因为我是新手,我希望能得到任何帮助.否则,我会留下它,希望这可以帮助其他人!谢谢!

It seems to be working perfectly now. Please, anyone, let me know if this is a poor way of doing it, because I'm new to this and I would love any help. Otherwise, I'll leave it and hope this can help other people! Thanks!

这篇关于为什么添加对象后 UIScrollView 不会完全滚动?使用故事板、ARC 和 Xcode 4.5.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆