I have a UIScrollView with several buttons in it. The buttons at the bottom of the scrollview are visible, but don't respond when they're pressed. My problem appears to be the same as the one in this Stackoverflow Question, but the accepted answer does not seem to solve my problem; the frame of the scrollview, the contentSize of the scrollview, and the frame of the view inside the scrollview (which is also the superview of the problematic buttons) all have width 414pt and height 900pt. That should definitely be large enough to encompass the buttons which aren't responding.
EDIT:
I created the scrollview in Interface Builder. The edges of the the container view within the scrollview are pinned to the edged of the scrollview.
I tried adding these lines of code:
scrolling.frame = CGRect(x: 0, y: 0, width: 414, height: 900)
viewInsideScrollview.frame = CGRect(x: 0, y: 0, width: 414, height: 900)
scrolling.contentSize = CGSizeMake(414, 900)
self.view.frame = CGRect(x: 0, y: 0, width: 414, height: 900)
Adding these lines didn't change how the scrollview looked or acted (scrolling fine, but with unresponsive buttons on the bottom.)
A UIScrollView infers its size by the view/views inside of it. You created your views in the interface builder so that could be why the lines of code you added did not work (I am not entirely sure why they did not). However, what will work is setting constraints on your viewInsideScrollview so that the scrolling expands to the full content size. For example, if you have 10 buttons in your viewInsideScrollview, you should set a top space constraint on the top most button, leading and trailing constraints, constraints to give spacing in between buttons, and a bottom space constraint on the bottom most button. The scrolling will expand to accommodate all of the buttons, and you will be able to select all of them.
Related
I have a project where I'm trying to scroll horizontally using a UIScrollView however, I can't get it to scroll.
The scrollview was added through Interface Builder, and after that it was set up in viewWillAppear() this is what I did:
let overview = CourseDetails(course: self.course, type: holes, frame: CGRect(x: 0, y: 0, width: 812, height: 100))
scrollView.addSubview(overview)
scrollView.contentSize = CGSize(width: 812, height: 100)
overview.frame.size.width = 812
scrollView.translatesAutoresizingMaskIntoConstraints = false
The scrollview has a content view width of 812, while the UIViews frame is 345 in width...
Is there a step I'm missing in the whole scrollview scenario?
You don't need to set the content size of UIScrollView. You can set it in interface builder.
Follow these steps in interface builder:
Add a UIScrollView in your view hierarchy
Add a UIView as subview of UIScrollView which will be the content view of UIScrollView. Now You will add all other views for which you want scrolling as subviews of this contentView.
Now set constraints for both UIScrollView and it's content view(UIView).
For UIScrollView set Leading, Trailing ,Top and Bottom constraints to it's superView.
For contentView of UIScrollView, set leading, trailing, top and bottom constraint pins to UIScrollView.
Now the tricky part for setting content size of the UIScrollView. Set the width and height constraint for the contentView.
You have to set the width and height equal to UIScrollView. To set these two constraints, make selection on contentView and then control + drag to UIScrollView and then choose equal width and equal height.
Now set the constraint priority to low(250) of width if you want horizontal scrolling or of height if you want vertical scrolling.
I think, CourseDetails view generation arises problem. I check this using following code:
let overview = UIView.init(frame: CGRect(x: 0, y: 0, width: 812, height: 100))
overview.backgroundColor = UIColor.green
scrollView.addSubview(overview)
scrollView.contentSize = CGSize(width: overview.frame.size.width, height: 100)
scrollView.translatesAutoresizingMaskIntoConstraints = false
It's working. Please share CourseDetails view information for resolving this issue otherwise it's difficult to trace.
Please check interface builder Scroll View settings:
1. Enable check mark for scrolling enabled [Scrolling Section]
2. Enable check mark for user interaction enabled [View Section]
I'm using swift to build an application. I want to add some content to my view controller using storyboard.
I already put everything inside the scrollview and set the specific height, but however when I run the application, the scrollview always set longer than the button in the bottom of the view
I want the scroll view stop right after the button
Please kindly help me how to solve this problem
thank you
after scrolling
for your information, please ignore the white space between 'critics' and submit button, because actually there's UITextView between them but not visible (still try to add some border)
these are my storyboard preview
1) ScrollView Contraints
2) ScrollView -> contentView Constraints to scroll View same as above image
3) now ContentView width and Height Constraints to main View [SuperView in which ScrollView is embedded] and constraints will be as follows.
4) now click on the EqualWidth to View [Third constraint from top]and edit it as in step 6
5) contentView Width Contraint
6) ContentView Height Constraint // set priority [must] . here you need to change the first item and second item in the menu to as shown First as - ContentView.Height and second as - View.height and set priority to 250 after this a dotted line will appear in storyboard along the contentView
7) now add the content like [UIView, labels, textfields] in contentView and add constraints as Top upperMost view top space to contentView [like I have]DoubleRight imageView
and constraints for my DoubleRight imageView are
look for the Top space margin its given a top space 20 points
and same you need to do for the last item you will be adding in ContentView like I have uiView
add bottom space from this respective view to your superView[ContentView] and my constraints are:
after you had initialed all these steps results will be as Expected for every Screen size no need to change height additionally for screen sizes
Note : - [all the views must be connected to each other with top and bottom parameter]
like Flow will be
View1 - top to contentView and bottom to View2
View2 - top to View1 and bottom to view3
View3 [Last view] - top to View2 and bottom to contentView as shown
using uiView, uiimageViews their heights must be fixed
It may be helped
your_scrollView.frame = CGRect(x: 0, y: 0, width: 375, height: 667)
your_scrollView.contentSize = CGSize(width: 375, height: 1000) // You can set height, whatever you want.
You can try :
your_scrollView.frame = CGRect(x: 0, y: 0, width: 375, height: 667)
your_scrollView.contentSize = CGSize(width: 375, height: self.view.frame.size.hight)
I'm trying to put an uiview over the main view controller. The way i'm doing it, looks like this (the red view is the view, and the black one is the main view controller)
But i want it to look like this...
This is the code i have tried (the "viewww" is the red view)
viewww = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height - navBar.frame.height))
What i want is to have a space in the top (the height of a navbar) and not in the bottom when the "viewww" appears, but i don't know how to set the space in the top.
Thanks in advance!!!
This puts your view down to the height of the navBar
viewww = UIView(frame: CGRect(x: 0, y: navBar.frame.height, width: self.view.frame.width, height: self.view.frame.height - navBar.frame.height))
The cleanest way to do this, without hard-coding navigation bar heights, is to use autolayout. Constrain the view's leading and trailing edges to the superview's, and constrain the view's top and bottom edges to the top layout guide and bottom layout guide (or, if you're using iOS 11, the Safe Area edges). See https://useyourloaf.com/blog/safe-area-layout-guide/ for a fuller explanation.
Couldn't find any answer for this, or am I missing something...
In iOS with autolayout on storyboard. If I have a UIView with some controls in it, depending on what the user clicks, the subviews are resizing. Can I somehow have the superview resize to the content?
In my app I have a view, but if a button is clicked subview is hidden I want the superviews height to decrease to exclude the hidden view. I can hook up the height constraint and handle it programatically, but it would be nice to handle it automatically....
Is there a way?
Can I assume that you have some constraint that relates the size of your superview to the size of your subview?
If so, all you will need to do to resize your superview when you hide the subview, is change the height constraint of your subview to 0 (auto layout won't care if your subview is hidden or not, it only takes into account constraints).
you can try something like this:
You can try putting this code to your buttonAction
yourView.frame = CGRect(x: anotherView.bounds.minX, y: anotherView.bounds.min, width: anotherView.bounds.width , height: anotherView.bounds.height)
in this case "yourView" will change its size to "anotherView".
yourView.frame = CGRect(x: anotherView.bounds.minX, y: anotherView.bounds.min, width: anotherView.bounds.width , height: anotherView.bounds.height / 2)
This example will change the height to 1/2 of "anotherView" frame.
Hope it will help you a little bit.
I was missing something.. 😄
What I didn't get was that the view will resize for its contents if you don't set a height for it and have the content have a constraint for the bottom of the view.
So I ended up setting the height of the hidden control to 0, and the bottom control constraint to parent bottom.
I got frustrated with AutoLayout so I decided to disable it and just set the positions and sizes of the elements in my view programatically. I tried this:
func setScrollViewBounds() {
var windowFrame = self.view.frame;
var scrollerHeigth = (windowFrame.height/100)*15
BottomScroll.frame = CGRect(x: CGFloat(10), y: windowFrame.height-scrollerHeigth, width: windowFrame.width-20, height: scrollerHeigth);
TopScroll.frame = CGRect(x: CGFloat(10), y: BottomScroll.frame.maxY-10, width: windowFrame.width-20, height: scrollerHeigth)
}
Im trying to set the two scroll views (TopScroll and BottomScroll) to be at the bottom of the page and have one be on top of the other. This however doesn't seem to do anything. Any ideas why?
I'm calling the function in ViewDidLoad().
TopScroll and BottomScroll are IBOutlets.
Apply constraint following order
Put your subviews in UIView Object(Which will subview of ScrollView).
Select Both ScrollView and UIView
Go->Pin->Equal Width.(Not Equal Height)
Select UIScrollView Go-> Pin uncheck Constraint to Margin and give Top,Bottom,Left,Right constraint.
Now Select UIView(Subview of UIScrollview say contentView) control click contentView to Main View(Default controller's View) and select Leading,Trailing,Top,Bottom constraint.
Finally Select contentView Go-> Resolve Auto-layOut issue -> Reset to Suggested constraints.
Constraints for subview of ContentView as usual with respect to contentView. use viewDidLayoutSubview for setting content-size to UIScrollView.