UIScrollView Autolayout可防止垂直滚动

时间:2013-07-18 17:52:48

标签: uiscrollview autolayout

我想知道如何使用autolayout在UIscrollView中裁剪图像 我正在尝试使UIscrollView只能水平滚动。如果图像高于视图高度,则应裁剪。我已经尝试了很多属性,但无法使uiscrollview中的所有图像与视图的高度相同,以避免垂直滚动。 我错过了什么吗?

#import "WelcomeController.h"

@interface WelcomeController ()
@property (strong, nonatomic) UIScrollView *scrollView;
@property (nonatomic,strong) NSArray *contentList;
@end

@implementation WelcomeController
@synthesize contentList =_contentList;

- (void)updateUI
{

    UIScrollView* sv = self.scrollView;

    id previousLab = nil;

    for (UIView *lab in _contentList) {
        lab.translatesAutoresizingMaskIntoConstraints = NO;
        lab.backgroundColor = [UIColor blackColor];
        lab.contentMode = UIViewContentModeScaleAspectFit;
        [sv addSubview:lab];
        [sv addConstraints:
         [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[lab]|"
                                                 options:0 metrics:nil
                                                   views:@{@"lab":lab}]];
        if (!previousLab) { // first one, pin to top
            [sv addConstraints:
             [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[lab]"
                                                     options:0 metrics:nil
                                                       views:@{@"lab":lab}]];
        } else { // all others, pin to previous
            [sv addConstraints:
             [NSLayoutConstraint
              constraintsWithVisualFormat:@"H:[prev][lab]"
              options:0 metrics:nil
              views:@{@"lab":lab, @"prev":previousLab}]];
        }
        previousLab = lab;
    }
    // last one, pin to bottom and right, this dictates content size height
    [sv addConstraints:
     [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[lab]|"
                                             options:0 metrics:nil
                                               views:@{@"lab":previousLab}]];
    [sv addConstraints:
     [NSLayoutConstraint constraintsWithVisualFormat:@"H:[lab]|"
                                             options:0 metrics:nil
                                               views:@{@"lab":previousLab}]];

}

-(void)setContentList:(NSArray *)contentList
{
    _contentList = contentList;
    [self updateUI];
}

- (void)setupScrollView
{
    UIScrollView* sv = [UIScrollView new];
    sv.backgroundColor = [UIColor redColor];
    sv.translatesAutoresizingMaskIntoConstraints = NO;
    sv.pagingEnabled = YES;
    sv.showsHorizontalScrollIndicator =NO;
    sv.showsVerticalScrollIndicator = NO;
    sv.bounces =NO;
    [self.view addSubview:sv];
    [self.view addConstraints:
     [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[sv]|"
                                             options:0 metrics:nil
                                               views:@{@"sv":sv}]];
    [self.view addConstraints:
     [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[sv]|"
                                             options:0 metrics:nil
                                               views:@{@"sv":sv}]];
    self.scrollView = sv;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupScrollView];

    //for testing
    UIImageView *image1=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"welcome1.jpg"]];
    UIImageView *image2=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"welcome2.jpg"]];
    UIImageView *image3=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"welcome3.jpg"]];
    self.contentList = [NSArray arrayWithObjects:image1,image2,image3,nil];
}



@end

3 个答案:

答案 0 :(得分:1)

您是否尝试过明确设置滚动视图的高度?

  

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@“V:[_ sv(123)]”options:0 metrics:nil views:views]];

您需要将(123)以上的高度替换为您需要的高度,显然还需要views

答案 1 :(得分:0)

Autolayout自动管理UIScrollView的{​​{1}}(可滚动区域)。因此,如果您在其中使用大于高度的内在大小的子视图,则会增加contentSize。我可以想到两件事:

  1. 将图像粘贴在与滚动视图高度相同的普通contentSize中。

  2. UIView进行子类化并覆盖UIImageView方法以返回所有图像的固定高度。不过,这似乎是一个糟糕的解决方案。

答案 2 :(得分:0)

我认为您应该能够将图像视图的帧设置为固定高度(通过约束)。然后添加约束以使图像视图的顶部和底部固定为来自滚动视图的x常量。

这将让scrollview知道要使用的确切内容大小。然后,只要它的框架(由你给出的与其超视图相关的任何约束确定)是> =图像视图的固定高度,它将不会垂直滚动。