1

我想知道如何使用自动布局在 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
4

3 回答 3

1

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

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_sv(123)]" options:0 metrics:nil views:views]];

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

于 2013-07-19T11:45:24.220 回答
0

Autolayout 自动管理UIScrollView's contentSize(可滚动区域)。因此,如果您将内部尺寸大于高度的子视图粘贴在其中,它将增加contentSize. 我能想到两件事:

  1. 将图像粘贴在UIView与滚动视图相同高度的平面中。

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

于 2013-07-19T11:26:41.087 回答
0

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

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

于 2014-03-05T14:00:39.360 回答