【问题标题】:Scroll horizontally with UIScrollView don't seems to work with Autolayout使用 UIScrollView 水平滚动似乎不适用于 Autolayout
【发布时间】:2013-01-18 06:46:07
【问题描述】:

我正在尝试使用UIScrollViewUIImageView 创建一个水平滚动。图像视图的宽度为 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

【问题讨论】:

    标签: ios objective-c uiscrollview uiimageview autolayout


    【解决方案1】:

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

    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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-03
      • 2022-01-04
      • 2014-09-14
      • 2013-10-15
      • 1970-01-01
      • 1970-01-01
      • 2020-02-20
      相关资源
      最近更新 更多