使用UIScrollView水平滚动似乎不适用于Autolayout

时间:2013-02-03 11:31:26

标签: ios objective-c uiscrollview uiimageview autolayout

我正在尝试使用UIScrollViewUIImageView创建水平滚动。图像视图的宽度为1280像素(4页),scrollView为320px。当我使用此代码没有自动布局模式时,滚动正常工作:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.scrollView.scrollEnabled = YES;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    CGRect imageFrame = self.helpImageView.frame;
    NSLog(@"imageFrame w: %f",self.helpImageView.frame.size.width); //prints 1280.000000
    [self.scrollView setContentSize:imageFrame.size];
    NSLog(@"contentSize w: %f",self.scrollView.contentSize.width); //prints 1280.0000
    NSLog(@"scrollFrame w: %f",self.scrollView.frame.size.width); //prints 320.00
}

但是,如果我想使用Autolayout,此代码返回相同的结果,但UIScrollView不会滚动。为什么???我正在使用这些约束:

Bottom space to: ScrollView Equals:32.0
Leading space to: ScrollView
Trailing space to: ScrollView
Top Space To: SCrollView Equals:-20.0
Bottom Space To: View Equals:-12.0
Leading space to: View
Top Space To: View Equals:427.0
Trailing space to: View

1 个答案:

答案 0 :(得分:0)

我还创建了水平滚动视图,然后初始值如下。

CustomScrollView.m:

self.showsHorizontalScrollIndicator = NO;
self.showsVerticalScrollIndicator = NO;
self.scrollsToTop = NO;
self.delegate = (id)self;
self.contentSize = CGSizeMake(320.0 * PAGE_COUNT, 420.0);
self.pagingEnabled = YES;
self.bounces = NO;

for (int i = 0; i < PAGE_COUNT; i++) {
//  NSString *str = [NSString stringWithFormat:@"%@%d%@", @"foo", i, @".png"];
    UIImageView * imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:str]];
//  imgView.frame = CGRectMake(320.0f * i + 10, 0, 300, 300 * 1.33);
    [self addSubview:imgView];
}

在这个项目中,PAGE_COUNT = 4,4个图像是foo0.png~foo3.png。