Swift - contents within scrollview get chop off - ios

Facing an issue when scrolling, a part of content didn't displayed.
https://i.stack.imgur.com/70FIC.png
Here it is what is supposed to be displayed
https://i.stack.imgur.com/ViPjF.png
My UI is designed in storyboard, the structure for the scrollable component is as below:
UIView -> UIScrollView -> UIStackView -> Multiple UIViews
If wondering how I take the screenshot of the one that's supposed to be displayed, the price is a drawer component, somehow after I tapped the drawer component, the content will be displayed, but after I scroll to top, same issue will happen to the content, which they get chop off.
https://i.stack.imgur.com/OKSEY.png
Updates:
Trying to put background colour on views to see which view is blocking the scrollview.
https://i.stack.imgur.com/vZBY7.png
Found that the scrollview (Booking DetailsSV) blocked by its superview (Booking Details View) in the hierarchy. Here is the screenshot taken in the test phone.
https://i.stack.imgur.com/Wm7xa.png
Is it suppose to happen like this?

Your scrollview doesn't seem to have correct contentSize. You need to set content size of your scrollView like following:
let sizeOfContent = 500
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, sizeOfContent);
You can do this in viewDidLoad.

Related

Xcode embed all direct in scrollview without content view

I'm using Xcode 6.4 and I'm struggling with a large page with a bunch of various labels and text fields etc., that has to be scrollable. I looked at various tutorials some of which are obviously outdated and I had all my elements at first in a view that is inside of a scroll view. But that was a big pain with getting all the elements aligned properly using Auto Layout, in addition to not getting it to scroll either.
Now I see one person recommend using the following directly on the elements, without having a content view at all just a scroll view:
Editor -> Embed In -> Scroll View
I like it for the fact that nothing seems out of proportion, all the elements are shown on the iPhone exactly as I have it on the Storyboard without having to set any constraints, however I'm not able to get it to scroll.
This is my class
class ResNotFoundViewController: UIViewController, UIScrollViewDelegate
and I also try to set the height of the scrollview in the code as so:
#IBOutlet weak var scrollviewoutlet: UIScrollView!
and then this in the viewDidLoad as so:
scrollviewoutlet.contentSize = CGSizeMake(375, 840)
.
But I'm not able to get it to scroll at all, it will stay exactly like it opens up with From: Label being the last visible elements.
What am I doing wrong?
You've located the problem, thanks to your logging. (Don't worry about the double call - how many times and when viewDidLayoutSubviews gets called is uninteresting.)
The problem is that you've set the scroll view itself to be taller than the window. In particular, it is 375x809. But so is its contentSize. Thus there is nothing to scroll. A scroll view can only scroll if its contentSize is bigger (in one or two dimensions) than the scroll view. (Moreover, a scroll view bigger than the screen is usually somewhat pointless, since there will be parts of its content that the user will never see - even scrolled all the way, that content will be offscreen.)
So, fix the size of the scroll view itself and you'll be fine. The best way is to assign it constraints that pin its boundaries to its superview, since the superview will have different sizes on different devices.

Swift - Can't understand the contentSize of a UIScrollView

Hi I have set in my storyboard a UIScrollView (of the same size of the view) and ctrl-dragged it in my code.
Now I have created a button programmatically using a simple algorithm of mine.
I then tried to
println(button.frame.origin.y)
which printed 1700.
Then I tried
println(button.frame.height)
which printed 142.2 (also coming from the algorithm). So the heigh I would like to reach while scrolling is 1700 + 142.2 = 1842.2
So i tried to hardcode the content size in this way
self.scrollView.contentSize = CGSize(width:self.view.bounds.size.width, height:2000)
But when I run the app I can scroll no more than the middle of the button, which is in numbers
1700 + 142.2/2 = 1771.1
Why the heck is that?
I did all this to try to understand how this UIScrollView works but I can't figure it out.
EDIT
Added a screenshot.
Inside the bubbles there is the button.frame.origin.y value. That's the max I can scroll leaving the screen untouched.
This is probably to do with scroll view insets - a mechanism where the edges of the content always appear under the navigation and toolbar.
You either need to manually account for this by reading the contentInset property of the scroll view, or using interface builder.
Scroll view Reference:
https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/UIScrollView_pg/CreatingBasicScrollViews/CreatingBasicScrollViews.html

How can I change UITableView height to its content height and still have clickable content?

So I have a tableview where I changed the size to match its content size with this code.
CGRect testF = table1.frame;
testF.size.height = table1.contentSize.height;
table1.frame = testF;
But when I try to click any of the items I can only click the top two or the section in the red, because that is the initial height of the table in the beginning.
Is there a way around this, to dynamically change its height and still keep it clickable?
I would think that the problem is not that you re sized the tableview, but that there is something that is blocking those touch events. Perhaps you have some other view in the way or the tableview is nested inside of another view which has a frame set to the tappable area that you see but has clips to bounds off so you are able to see the tableview outside of it. Try doing something like the following and see if it works, this should test my theory out:
table1.superview.frame = cgrectMake(table1.superview.frame.origin.x, table1.superview.frame.origin.y, testF.size.width, testF.size.height);
Add this under the code you showed above.

UITextView won't scroll from outside particular area

I have a UITextView with approx 100 lines(for testing purpose its static) which scrolls fine but when i try to scroll from approx 150 its from the bottom i can not scroll. That means it lets me scroll from only certain area of the textview. for example see the image below. If i try to select and scroll above 3 line from top, i can scroll but below 3rd line, It won't let me scroll. I went through documentation but couldn't find anything that makes sense.
Try resizing the frame of your UITextView so it does not overlap the UITabBarView. If you create this in interface builder, you can resize it there.
if there is some area that is not taking ui touches, thAT means, your UITextView is being overlapped by another view. try:
[self.view bringSubviewToFront:textView];
also think about Enrico's answer.
Put Background color for TextView. It will give you extact idea of TextView Complete framing and is it sub view of background or foreground.

What is the best way to animate the size of sub view in a UIScrollView?

I've got a scrollview that allows the user to scroll between different pages and then tap on one to have it expand so that they can read the page in full, a little like how one changes tabs in Safari on the iPhone. Changing the frame size of each sub view is a bit of a pain when rotating as the scroll position is getting lost as the content size of the sub view has to change too. I was wondering if there was a more effective way of
resizing the views for entering 'viewing' mode or rotating the device.
The solution to your first problem is when you want to expand the view, pull it out of the scrollView then add it to self.view.subviews with an appropriate frame, then animate the frame to fill the screen. When done with it do the reverse, shrink it, then when its back to the appropriate size stick it back in the scrollView.
If for some reason this does not work - the scrollview is showing other stuff that gets moved when you remove the view, then instead of just removing your view from it, create a simple UIView of the same size as the view you will expand, and essentially replace the view you pull out with the "placeholder" view.

Resources