iOS:自动布局导致 UIScrollView 不滚动

iOS: Autolayout causing UIScrollView to not scroll(iOS:自动布局导致 UIScrollView 不滚动)
本文介绍了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 上调用了以下方法,在这里我设置"了我的滚动视图(itemList 是我的滚动视图属性,itemNames 是一个包含图像名称的数组):

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 :)

推荐答案

两种解决方案:

  1. 创建可以同时满足的不同约束(您必须进行编辑).我认为问题在于您的底部空间和顶部空间限制是相互排斥的.请删除一个,然后重试.如果这对您来说很困难,请尝试添加另一个 UIView 来包含 UIScrollView 以帮助管理您的约束,起初可能看起来很奇怪,但有时添加另一个视图来包含您的view 实际上在每个级别都让它变得更简单.

  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 不滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)