1

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

- (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
4

1 回答 1

0

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

自定义滚动视图.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。

于 2013-02-04T01:31:48.753 回答