ios 自定义 UITableViewCell 内的水平 UIScrollView - 使用 IB Storyboard - 不滚动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19185446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:04:49  来源:igfitidea点击:

Horizontal UIScrollView inside custom UITableViewCell - using IB Storyboard - Not scrolling

iosobjective-cuitableviewuiscrollview

提问by marco alves

The main goal is to be able to scroll each row's content horizontally.

主要目标是能够水平滚动每一行的内容。

I'm trying to do this with X Code 5 and using StoryBoard.

我正在尝试使用 X Code 5 并使用 StoryBoard 来做到这一点。

The problem seems to be simple, but after many hours of searching I got nothing except for one problem that is somewhat similar but using programatic only approachsee here.

问题似乎很简单,但经过数小时的搜索,除了一个有点相似但仅使用编程方法的问题,我一无所获,请参见此处

In IB, I have the structure as shown in the design below. View structure

在IB中,我的结构如下图所示。 查看结构

  • The content size of the scrollview in set to {5000, 500} set both in IB and in code
  • The scrollview frame is {0,0}{320,44}
  • The labels frame is set to {20,0}{500,44}
  • 设置为 {5000, 500} 的滚动视图的内容大小在 IB 和代码中都设置
  • 滚动视图框架是 {0,0}{320,44}
  • 标签框架设置为 {20,0}{500,44}

I've also provided an example project in a github repository. The example also includes a "normal" scrollview working outside of a uitableviewcell.

我还在github 存储库中提供了一个示例项目。该示例还包括在 uitableviewcell 之外工作的“普通”滚动视图。

Please say that I overlooked something very basic.

请说我忽略了一些非常基本的东西。

采纳答案by Alexis

Autolayout is activated (and that's a good thing) in your storyboard. The scrollable size of a UIScrollView is computed based on the constraints of its subviews.

自动布局在您的故事板中被激活(这是一件好事)。UIScrollView 的可滚动大小是根据其子视图的约束计算的。

So you need to add "top, bottom, leading, trailing space to superview" on the UIImageView inside the UIScrollView. They can be all set to 0.

因此,您需要在 UIScrollView 内的 UIImageView 上添加“顶部、底部、前导、尾随空间到超级视图”。它们都可以设置为 0。

Now if you don't add any constraint to the UIImageView then its intrinsicContentSize size will be used.

现在,如果您不向 UIImageView 添加任何约束,则将使用其内部内容大小大小。

Your constraints should look like this:

您的约束应如下所示:

autolayout constraints

自动布局约束

Note that you were also missing constraints on the content view of your cell.

请注意,您还缺少对单元格内容视图的约束。

When you're done with the constraints, remove all the setContentSize: calls from your code.

完成约束后,从代码中删除所有 setContentSize: 调用。

回答by marco alves

Another alternative for this is to use UICollectionView inside a custom UITableViewCell

另一种替代方法是在自定义 UITableViewCell 中使用 UICollectionView

回答by steffenhk

I had the same problem, and found that removing the scrollview's content view and adding it again works:

我遇到了同样的问题,发现删除滚动视图的内容视图并再次添加它可以工作:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.label removeFromSuperview];
    [self.scrollView addSubview:self.label];
    self.scrollView.contentSize = self.label.frame.size;
}

It's not the most elegant solution, but it works for me.

这不是最优雅的解决方案,但对我有用。

回答by Yimin Rong

Ensure setContentSizeis called in viewDidLayoutSubviewsand in scrollViewDidEndZooming:withView:atScale:. In iOS6, if you didn't have the code in these methods, the default behaviour worked okay, in iOS7 with the new layout system, they are required. It may be called more than once for a setup, just keep calling setContentSize.

确保setContentSizeviewDidLayoutSubviews和 中被调用scrollViewDidEndZooming:withView:atScale:。在 iOS6 中,如果您没有这些方法中的代码,则默认行为可以正常工作,在具有新布局系统的 iOS7 中,它们是必需的。它可能会被多次调用以进行设置,只需继续调用setContentSize.

回答by Beto

I fix the situation via IB.

我通过IB解决了这个问题。

I must have the same value inside "Identity Value section - User Defined Runtime Attributes" with key "frame - Rect - CGRectValue" equal to "Size inspector - View values".

我必须在“身份值部分 - 用户定义的运行时属性”中具有相同的值,关键是“frame - Rect - CGRectValue”等于“Size inspector - View values”。

I hope this help you.

我希望这对你有帮助。

回答by Horst

I have the same problem today. After a few hours keep finding, it is solved by changing the cell's selection back to Default from None. Hope that it would help the others.

我今天也有同样的问题。几个小时后继续查找,通过将单元格的选择从无更改回默认值来解决。希望它会帮助其他人。

回答by Alex Cio

I can't access your Storyboard, with my xCode 4.

我无法使用Storyboard我的 xCode 4访问您的.

First of all, you should connect the UITableViewwith your ViewController. The UITableViewCellis inside your UITableView. Thats why you should try to change the settings of your UITableView. After connecting, you should try to change the contentinsetof your UITableViewlike this:

首先,您应该将UITableView与您的ViewController. 该UITableViewCell是你的里面UITableView。这就是为什么您应该尝试更改UITableView. 连接后,你应该尝试像这样改变contentinset你的UITableView

[self.tableView setContentInset:UIEdgeInsetsMake(0, 0, 2500, 2500)];

If this doesn't work, you could also try to change the content size of your TableView. Maybe you also have to change the size of your UIViewbefore it will work.

如果这不起作用,您还可以尝试更改 TableView 的内容大小。也许你还必须UIView在它起作用之前改变你的大小。

回答by Yimin Rong

With its new autolayout mode, iOS 7 seems to have broken a lot of iOS 6 and earlier apps that use UIScrollView. I posted a similar question recently [now put on hold by the moderators - so you might not be able to see it]where user Lv.BeLeCk found success using:

凭借其新的自动布局模式,iOS 7 似乎已经破坏了许多使用 UIScrollView 的 iOS 6 和更早版本的应用程序。我最近发布了一个类似的问题[现在被版主搁置 - 所以你可能看不到它]用户 Lv.BeLeCk 使用以下方法获得了成功:

self.automaticallyAdjustsScrollViewInsets = NO;

in the view controller holding the scrollview. Just put it after the scroll view's instantiation. See if that works, and if it does, please upvote his contribution.

在持有滚动视图的视图控制器中。把它放在滚动视图的实例化之后。看看这是否有效,如果有效,请为他的贡献点赞。

I think the root cause is that upon instantiation UIScrollViews are now updating their sizes based on their current contents, as opposed to before where they just left them the Hell alone.

我认为根本原因是在实例化时 UIScrollViews 现在会根据它们当前的内容更新它们的大小,而不是之前它们只是让它们独自呆在地狱里。

Why Apple wanted to muck with that I don't know. I'm still waiting for top and bottom alignment for multi-line labels!

我不知道为什么苹果想要搞砸。我还在等待多行标签的顶部和底部对齐!