Horizontal UIScrollView inside custom UITableViewCell - using IB Storyboard - Not scrolling
Solution 1
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:
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.
Solution 2
Another alternative for this is to use UICollectionView inside a custom UITableViewCell
Solution 3
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.
Solution 4
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
.
Related videos on Youtube
marco alves
Updated on August 05, 2022Comments
-
marco alves almost 2 years
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.
- 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.
-
Beto over 10 yearsDo you found the solution? I have similar situation. My scrollview works perfect inside the UIViewController, but when I put inside a UITableViewCell simple does not work. I works with Xcode 5 and iOS 7.
-
marco alves over 10 yearsNot yet. But I have not tried the solution proposed by Yimin. If you tried first, please report here.
-
iosMentalist over 10 years@marcoalves Hello, any updates ? did you fix it?
-
marco alves over 10 years@Shady No. I abandoned the ideia in favor of another UI concept -- a more familiar concept.
-
iosMentalist over 10 years@marcoalves can you share it ?
-
marco alves over 10 years@Shady I used this: github.com/eaigner/COPeoplePickerViewController
-
marco alves over 10 yearsIt does not seem to work. I wonder if it has to with the scrollview being inside the uitablecellview
-
marco alves over 10 yearsI 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 over 10 yearsThanks 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.
-
dmzza about 10 yearsThis 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.
-
Rafał Sroka about 10 years@Yimin can you please link to the documentation stating that the above mentioned methods re required to be put in
viewDidLayoutSubviews
? -
Yimin Rong about 10 years@reecon - I came to the conclusion after reviewing numerous comments on SO. e.g. stackoverflow.com/questions/13439304/…. At the time of writing, this was the case, maybe it's different now.
-
Deepak Thakur about 8 yearsyeah. following tutorial might help. ashfurrow.com/blog/…