UIButtons at the bottom of UIScrollView not working - ios

I created a UIScrollView in my storyboards and have added 12 UIButtons in a container View which is inside the UIScrollView.
when running on the iPhone 5s simulator, 9 of the buttons can be seen on the screen, the rest of the buttons only can be seen when you scroll down.
the 9 buttons that can be initially seen on the screen can be interacted with. However the 3 buttons at the bottom of the scroll view (which have to be scrolled to in order to be seen) cannot be interacted with.
I ran the app on an iPhone 6 simulator which displays all 12 of the buttons on the screen without having to scroll and the bottom 3 buttons work.
I am using autolayout.
I have tried fiddling with the contentSize of the UIScrollView and it does not help.

It seems you need to increase the frame height of container view. The contentSize of scrollView only affects how it will scroll, which is irrelevant here.
If the button is outside the container view, it will still show up. However, it can't respond to any touch event.

when using scrollview in storyboard with autolayout. it is usually set up using a content view inside the scrollview that will contain all the other views (e.g. buttons, labels, etc.) the content view are usually set to have equal height and equal width.
to make your content view expand it's height, you need to add a height constraint then assign it to an outlet so you could easily manipulate it's value with in your code. then set the content view's height constraint priority to 1000 ("required") and set the content view's "equal width to:view" constraint priority to 750 (high).

For me it was all just to Uncheck the checkbox Adjust Scroll View Insets in the storyboard of that ViewController.
PS: The above answers didn't worked for me.

Another option is to reduce the priority on the Content View.Center Y constraint.
In Xcode 9, I laid out the contentView as needed and the button extended below the view on the iPhone 5. I dynamically resized the scrollView.contentSize.height and contentView.frame in the view controller. The scrollView scrolled for me but I was still unable to select the button and was seeing warnings in the Storyboard.
Once I lowered the priority on the Center Y Alignment Constraint for the contentView, the warnings in Storyboard disappeared and I was able to scroll to the bottom of the scrollView and select the button on the iPhone 5 simulator.
NOTE: I'm still checking the device size and resizing the scrollView.contentSize.height value in viewWillLayoutSubviews() for small devices.
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
scrollView.contentSize.height = (UIDevice.current.isSmallDevice) ? 560 : contentView.frame.size.height
}
(UIDevice.current.isSmallDevice is a call to an extension of UIDevice that returns a bool determined by screen size)

Just increase the container view height and also Container view must be a subview of the scroll view and all other subviews are part of the container view.
like Scroolview -> ContainerView--> UIButtons

Add all subviews from container to ScrollView. And hide the container. Make sure container(ContainerView of scrollView) should also be a subView of scrollView.

Add height constraint to contentView.
Lower the priority of center Y to superview to 250
Lower the priority of bottom space to superview to 250
Increase the height of contentView in code by setting the height constraint constant value.

Related

autolayout view gets overlapped on smaller devices

Basically I have one view with some height x
and 4 other views with same height y all these 5 views are vertically one after another with 10 px space
all these views have again some child views which depends on them.
For this scenario stackview is ideal but it support starts from iOS 9 unfortunately I have to support from iOS 6
so basically I fixed all the views with leading,trailing,top,bottom and height constraint...
it works good in iPhone 7s ... but in iphone 4s the view gets overlapped on each other
in iphone se the views are very much congested..
I was always thinking the height will change according to the screen size for iPHone 4s but that didn't happen
How can I work this out ?
There are two ways to solve the problem you are facing.
First:
Instead of giving a fixed height to any of your UIView you can use a proportional height constraint. Select your UIView and control + drag from your UIView to the main superview. Select equal height constraint. Now, double click the height constraint and set the multiplier to 0.15 or any value you seem nice. This will ensure the view thus created is always 0.15 times the entire view's height. Now you can create your other views either proportional to this view following the same steps or to the superview.
Second:
(I prefer this approach as for items as sometimes you need to create forms the above approach might still push elements off the screen).
This will use a UIScrollView. To your main view add a UIScrollView and add a leading, trailing, top and bottom constraint to your superview. Add a UIView to this UIScrollView and give it a leading, trailing, top and bottom as well. Additionally give it a equal height and width constraints to the superview of the UIScrollView with a low priority of like say 250. Now add all your elements inside this UIView however you seem fit. With fixed height, proportional height whatever and happily run it. But ensure you add a bottom of >= a minimum value for your bottom most view.
The screen will automatically become scrollable if the content will go off screen otherwise it won't be scrollable at all.
fixed all the views with leading,trailing,top,bottom and equal height constraint instead of only height(not give fixed height to any view)

Scroll View Size not match with the device screen in xcode

Ok, I tried the page with several buttons but all of them will not fit in one screen. So, I create the scroll view and the view and then put all of buttons inside the view. The storyboard look fine. Other devices such as iPhone 5 and 5S look fine but when I tried 6 and 6 plus, the size of scroll view was incorrect. I couldn't figure what went wrong.
You have to modify constraint for
scrollview with uncheck constraint to margin with all left right
bottom and top is zero
Container view to super view set equel width constraint
container view to scrollview set horizontal center constraint.
Each UIBUtton set top/left/bottom with constant height not set bottom constraint.

UIScrollview is not scrolling beyond UIView height even though UIView frame is increased

I am trying to create a screen where scrolling is required in iphone 3.5 inch screen . I have attached images which contain the configuration of uiviewcontroller , uiscrollview , uiview . The problem is eventhough the height seems to increase , when i run iPhone 4s , some part of scroll is hidden beyond the UIView (Although uiview height shows 700 when i print the height in console) . What might be the problem ? Please help me ?
You have constraints that have negative values. On the last screenshot, which I assume are the constraints from the view inside the scrollView, the bottom offset to the superview is -400.
I think this is why you don't see the entire view because the bottom offset is negative. Try setting this value to 0, this should fix it.
If this does not fix your problem, maybe you could specify for which view the constraints from the images are.
Let me know how it goes! Good luck!
When you work with UIScrollView and AutoLayout it's always tricky.
You have to add all your subviews to one UIView and add that view to UIScrollView.
Autolayout Constraints you need:
For UIScrollView:
Left to Superview
Right to Superview
Top to Superview
Right to superview
For Inner View of UIScrollView (i.e The only view inside ScrollView):
Width & Heigh
Left, Right, Top and Bottom constraint to Superview (i.e. ScrollView)
Also add Horizontal Centre to Superview.
And it'll work like charm (Works every time for me at least:)
The contentSize must be larger than the scrollview's frame for there to be anything to scroll. :)
In this case, they're both (600, 1300) so change your scrollView's height to (320,480)
It's a bit confusing, but as Yuvrajsinh told you, you need to set the auto layout constraints for your scroll view to its 4 sides to the superview. Then do the same with the content of the scrollView, and the most important: Set a fixed/not related with scrollview height and width for the content
Thanks for the information and suggestions . Now it is working correctly . What i did is
set scroll view top ,left , right , bottom aligned to superview
Set content view of scroll view to top , left , right , bottom aligned to scrollview
set the content view of the scroll view to "Equal Heights" and "Equal widths" to the top superview.
Then i set the equal height constraint to have low priority of 250
Now i can scroll whenever required based on the size of the phone screen :)

UIScrollView content size fixed to 600x600(Main.storyboard size)

I've been facing this issue from past 2 weeks and not yet got a solution.
I'm using UIScrollview in my application where the problem exists.
Generally in my app, there is dynamic text and images with different sizes will come from webservices. For example, imagine the Facebook Newsfeed. My application is similar to the Facebook newsfeed. Sometimes, there will be only text, sometimes there will be text and images. And comments for that post.
As i've seen in many links, the heirrarchy i'm following is SuperView-->UIScrollView-->Content View and the elements are placed in that content view. I'm assigning the constraints from the elements in the contentview to the Superview (ContentView --- constraints -- SuperView). When the content in the view exceeds the size of the superview, it has to scroll. But the scrollview content size is limited to the size of 600x600 i.e., in main.storyboard, when we design for W any x H any size. I've seen many tutorials and searched many sites. But following them gives me no luck.
Any help is appreciable.
Here is how to set up a scrollView in Interface Builder from scratch that works with Auto Layout.
Start with a new ViewController. Drag out a scrollView such that it fills the view. Untick Constrain to margins and constrain the left, top, right, and bottom edges of this scrollView to the left, top, right, and bottom of its superview with offsets of 0. This allows the scollView to fill the screen on any device in any orientation. You can make your scrollView take up less of the screen if you like, just make sure it is fully contrained.
Add a view to your scrollView. This should be the only top level view on your scrollView and it will serve as your contentView.
Constrain the left, top, right, and bottom edges of this contentView to the left, top, right, and bottom of the scrollView with offsets of 0. At this point, you will see warnings about ambiguous content size. That is because you haven't told it yet how big your content view will be.
To size the contentView, add width and height constraints to the contentView. If you want it to scroll, the width and height must be larger than the width and height of the scrollView itself. If you only want to scroll vertically, set the width of the contentView to be equal to the width of the scrollView. To do this, in the Document Outline view, control-drag from the contentView to the scrollView and select Equal Widths from the pop up.
If you want to be able to change the height of your contentView from code (to account for dynamic content), first create a height constraint for your contentView by control-dragging within the contentView and selecting Height from the pop up. Create an IBOutlet to the height constraint by control-dragging from the height constraint (found in the Document Outline view) to your ViewController's code. Give the outlet a name like scrollViewHeight then set the height with a value like scrollViewHeight.constant = 2000 when you need to change the scrollView's height.
Simply add your UIScrollView to your UIViewController's view in the storyboard and add the appropriate constraints in the interface builder.
Then, build your content view in the code and give it any frame you want, now start adding your controls to this content view and calculate the size of each one (especially the height), and at the end you have the total height of your controls.
Now set the frame of the content view to match that height and add it as a sub view to your scroll view, then set the content size in the code like this:
_scrollView.contentSize = contentView.frame.size;
Another note, do this changes to the sizes in the viewDidLayoutSubviews to avoid any problems with different screen sizes but be careful, this event is called many times so have a BOOL or something to ensure that the code that creates the view and add it to the scroll view is executed once, e.g. like this
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
if (! built) {
// Do everything I explained above here
built = YES;
[self.view layoutSubviews];
}
}

UIScrollView Not Scrolling in iPAD

I have a UiViewController, inside which I have a UIScrollView with the width and height as 768X3000. Inside the UIScrollView I have a view with widht and height as 768X1024.
I am totally confused on how to implement UIScrollView. My scrollView doesnt work. I have lot of contents to be displayed in the UIView and UIScroll doesnt scroll down.
What connections or setting do I have to do make the UIScrollView work.
You need to set the contentSize property of your UIScrollView to 768 x 3000, not its frame or its bounds. So in viewWillAppear you could add code to set your scrollView's contentSize:
self.scrollView.contentSize = CGSizeMake(768.0f, 3000.0f);
1- viewDidLoad is to soon. You must wait until after the layout of the views has taken place. Try setting the contentSize in viewDidAppear.
-(void)viewDidAppear:(BOOL)animated{
myScrollView.contentSize = CGSizeMake(768, 3000);
}
2- another tip you can set contentSize in viewDidLayoutSubviews:
3- set the delegate of your scrollview
myScrollView.delegate=self;
//in case you need the delegate methods
4- If still you cannot scroll the view even after you set contentSize correctly, make sure you uncheck "Use AutoLayout" in Interface Builder -> File Inspector. Reference
If you're using storyboards in Xcode 5 and developing for Auto Layout then there are a few constraints you need for it all to work (in my experience). First, lay your views out like this:
main view
scroll view
content view (put all the stuff you want to scroll in this view)
Then do the following in this order in the storyboard editor:
Set the content view height to 3000. Width should be 768.
Set the scroll view height to 1024. Width should be 768.
Set the main view height to 1024 if in freeform sizing or leave it using inferred sizing. Width should be 768.
Before you do the next steps, just double-check each view's height to be sure nothing changed. Sometimes the storyboard editor makes bizarre changes on its own.
Set a height constraint for the content view only. It should be set at 3000.
Pin the top, bottom, right, and left sides of the content view to the scroll view using 0 for each edge. You will have to manually change the bottom constraint from a negative number to 0. This is very important, so I'll repeat it: manually change the bottom constraint to 0.
Pin the top, bottom, right, and left sides of the scroll view to the main view using 0 for each edge.
Now it should scroll. If you want to ensure that it stays centered when you change to a horizontal orientation, add a horizontal center constraint to the content view as well.
I have many scrolling views in my iPad app and didn't have to use the .contentSize code once if I built my views this way in the storyboard editor.
Good luck! I know what an absolute pain and time-waster this can be.

Resources