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.
Related
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 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.
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.
When adding the code below the subview appears way out of where I intended it to be. It appears almost outside the parent view, and not inside as I expected. I am trying to add the view in viewDidLoad-method.
Parent view bounds:
(0.0, 0.0, 336.0, 200.0)
I am trying to add a view as a subview of this parent:
let subView = UIView(frame: CGRect(x: 200.0, y: 0.0, width: 100.0, height: 20.0))
Answer:
How can I get a view's current width and height when using autolayout constraints?
Following the answer fixes the problem without me understanding it fully. Isn't the size of the view updated when laid out by auto layout? Why do I need to invoke this method?
Before I invoke this method the width of the view is 336 points and after I invoke it it is 215 points.
I am using this code and its placing my view at the top of the viewing area instead of the bottom. Any idea what I am doing wrong? I am not creating the view in swift because I already have it created in IB with a toolbar and pickerview inside it.
let height = view.frame.size.height
yearView.frame = CGRect(x: 0, y: height - 250, width: self.view.frame.width, height: 250)
Even if I use the y coordinate and say 50 the view wont budge from the top. All I am trying to do is place this subview at the bottom of any sized phone's screen so they can select a value inside the view.
yearView.frame = CGRect(x: 0, y: height - 250, width: self.view.frame.width, height: 250)
Try moving it out of the ViewDidLoad, you need to position that view whenever you unhide it, otherwise it will not show.