iOS:自动展开导致UIScrollView不滚动 [英] iOS: Autolayout causing UIScrollView to not scroll

查看:177
本文介绍了iOS:自动展开导致UIScrollView不滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了一个 UIScrollView ,我想用它来显示水平布局的12张图片(只有8张适合屏幕)。在下面的图片你可以看到我有的问题(这使我的滚动视图不滚动),我的约束和我添加在故事板上的 UIScrollView

I have set up a UIScrollView with which I want to display 12 images (only 8 fit on screen) laid out horizontally. In the following image you can see the problem I'm having (which makes my scroll view not scroll), my constraints and the UIScrollView which I have added on storyboard:

我在 - (void)viewDidLoad 上调用了以下方法,其中设置我的scrollview和itemNames是一个数组与images'names):

I have called the following method on -(void)viewDidLoad, where I "set up"my scrollview (itemList is my scroll view property and itemNames a array with the images'names):

- (void)setupHorizontalScrollView
{
    self.itemList.delegate = self;
    [self.itemList setTranslatesAutoresizingMaskIntoConstraints:NO];

    [self.itemList setBackgroundColor:[UIColor blackColor]];
    [self.itemList setCanCancelContentTouches:NO];

    self.itemList.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    self.itemList.clipsToBounds = NO;
    self.itemList.scrollEnabled = YES;
    self.itemList.pagingEnabled = NO;

    NSInteger tot=0;
    CGFloat cx = 0;
    for (; ; tot++) {
        if (tot==12) {
            break;
        }

        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[self.itemNames objectAtIndex:tot]]];

        CGRect rect = imageView.frame;
        rect.size.height = 40;
        rect.size.width = 40;
        rect.origin.x = cx;
        rect.origin.y = 0;

        imageView.frame = rect;
        [self.itemList addSubview:imageView];
        cx += imageView.frame.size.width;
    }

    [self.itemList setContentSize:CGSizeMake(cx, [self.itemList bounds].size.height)];
}



我添加了[self.itemList setTranslatesAutoresizingMaskIntoConstraints:NO];因为我看到这个建议对其他职位,但它不工作与否。唯一的方法是,如果我取消选中在故事板上使用AutoLayout,但是移动 UIImageView 我使用看起来作为导航栏到屏幕的底部。
我不知道该怎么做,任何帮助是赞赏:)

I have added the [self.itemList setTranslatesAutoresizingMaskIntoConstraints:NO]; because I saw this suggestion on other posts, but it doesn't work with or without it. The only way it works is if I uncheck use AutoLayout on the storyboard, but that moves the UIImageViewI use to look as a navigation bar to the bottom of the screen. I don't know what to do anymore, any help is appreciated :)

推荐答案

p>

Two Solutions:


  1. 创建可同时满足的不同约束(您必须编辑)。我认为问题是你的底层空间和顶层空间限制是相互排斥的。请删除一个,然后重试。如果这对你很难,可以尝试添加另一个 UIView 来包含 UIScrollView 来帮助管理你的约束,

  1. Create different constraints that can be satisfied simultaneously (you will have to edit). I think the problem is your bottom space and top space constraints are mutually exclusive. please remove one and try again. IF this is difficult for you, try adding another UIView to contain the UIScrollView to help manage your constraints, it might seem odd at first, but sometimes adding another view to contain your view actually makes it simpler at each level.

关闭自动布局,并更改您自己的自动调整大小的掩码 UIImageView 是您想要的。

Turn off Autolayout, and change the autoresize masks of your UIImageView to be what you wish.

这篇关于iOS:自动展开导致UIScrollView不滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆