18

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.

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

  • 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}

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

Please say that I overlooked something very basic.

Community
  • 1
  • 1
marco alves
  • 1,707
  • 2
  • 18
  • 28

8 Answers8

8

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

marco alves
  • 1,707
  • 2
  • 18
  • 28
6

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.

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.

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

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.

Alexis
  • 16,629
  • 17
  • 62
  • 107
  • This worked for me, but because I am using a UIView in my scroll view instead of an Image View, I needed to set height and width constraints on my UIView as well in order for this to work. – dmzza Apr 04 '14 at 19:33
2

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.

steffenhk
  • 754
  • 5
  • 4
1

Ensure setContentSize is called in viewDidLayoutSubviews and 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.

Yimin Rong
  • 1,890
  • 4
  • 31
  • 48
  • Thanks Yimin. Actually, the apps features changed so I will not be needing this now. Nevertheless, I will be trying your suggestion on the github code to see how it goes. I will get back here hopefully with results soon. – marco alves Oct 21 '13 at 22:38
  • @Yimin can you please link to the documentation stating that the above mentioned methods re required to be put in `viewDidLayoutSubviews`? – Rafał Sroka Apr 24 '14 at 15:51
  • @reecon - I came to the conclusion after reviewing numerous comments on SO. e.g. http://stackoverflow.com/questions/13439304/uiscrollview-setcontentsize-breaks-view-with-auto-layout. At the time of writing, this was the case, maybe it's different now. – Yimin Rong May 12 '14 at 18:15
0

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

First of all, you should connect the UITableView with your ViewController. The UITableViewCell is 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 UITableView like this:

[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 UIView before it will work.

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
  • I don't quite get your answer. It seems your talking about the case where o insert a tableview inside a scroll view, which is not the case I'm asking. – marco alves Oct 04 '13 at 19:15
0

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:

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.

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

Yimin Rong
  • 1,890
  • 4
  • 31
  • 48
0

I fix the situation via 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".

I hope this help you.

Beto
  • 3,438
  • 5
  • 30
  • 37
0

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.

Horst
  • 1,733
  • 15
  • 20