所以,我知道我也有类似的问题,但可能并不准确(所以请不要给我打分——只是警告我什么的)。我已经搜索了几天来解决这个简单的问题。使用故事板、ARC 和 Xcode 4.5.2,我只需将一堆标签放在 UIScrollView 中并让它垂直滚动。我已经尝试过在 viewDidLoad、viewDidAppear 和 viewWillAppear 中设置帧大小和内容大小的多种组合,但无济于事。当里面没有任何东西时,滚动视图可以完美滚动,但是当我向它添加标签时,滚动只会滚动一个非常短的部分。
注意:我需要使用自动布局,否则我的整个项目都会搞砸。
这是我当前的代码...
.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 注释掉没有任何区别。
编辑:我在下面发布了解决方案。希望它能帮助别人!