Here is an image of what I'm envisioning.
Two bottoms at the bottom will always stay there. And height of textview and container view is dynamic. I will container another viewcontroller in container view. When sum of height of textview and container view is bigger than current viewcontroller's height, I want them to be scrollable.
I've been looking into 2 things: UICollectionView, UIScrollView.
However, I could not figure out how to make what I'm envisioning with those two components.
I also tried with stackview but that was not suitable for my purposes.
My gut tells me that UIScrollView is way to go but I cannot figure out. My current attempt was putting a scroll view and set the constraints to Nav bar and 2 buttons. Then, put textview and container view inside scroll view using storyboard.
If somebody can guide me a bit, that would be super helpful to me. Sorry for not containing any code, btw. I just couldn't even start :/
let margin = CGFloat(10)
let scrollView = UIScrollView(frame: view.frame)
let textView = UITextView(frame: CGRectMake())//Set textViewFrame
let containerView = UIView(frame: CGRectMake(x:margin, y: textView.height + margin, width: view.frame.width - 2 * margin, height: view.frame.height)
let button1 = UIButton(frame: CGRectMake(x:margin, y: textView.height + contentView.height + 2 * margin, width: (view.frame.width - 2 * margin) /2, height: 30)
scrollView.contentSize = CGSize(width: view.frame.width, height: button1.frame.origin.y + button1.frame.height + margin)
That should be enough to get you started. The important thing is adjusting the scrollView's content size to extend past the frame of the view. To test where this would get you you still need to add as subviews and you could also set backround colors different colors and adjust as necessary.
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.
For Example lets say I have a UIView I would like to display
view.addSubview(line)
Now I want t to be able to display that UIView in the top right corner. Let say inside the right side of the navigation bar. How would I do this? Or even in a specific place.
You will need to calculate the frame or need to give constraints to that view.
let screenSize = UIScreen.main.bounds
let screenWidth = screenSize.width
let screenHeight = screenSize.height
From above you can get screenwidth and you can set your y to 0. Let say width height of your view is 20. So, frame would be like below.
yourView.frame = CGRect(x:screenwidth - 20 , y: 20, width:20 , height: 20)
You can change the frame as per your requirements.
For navigation bar there is a better way to do it by adding right bar item. Look below code how to do it.
self.navigationItem.rightBarButtonItem = rightButtonItem
You can use
self.navigationController?.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: lineMat)
to set up a view on navigationBar right side.
Also you can add the lineMat view on UIApplication.shared.keyWindow
Just use UIApplication.shared.keyWindow?.addSubview(lineMat) and set up to lineMat view's frame.
Add a frame for your subview, as :
Objective-C:
line.frame = CGRectMake(0, 0, 50, 50);
Swift 3:
CGRect(x:0,y:0,width: 50,height: 50)
This will add your line subview on top left corner with height 50 and width 50.
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.