I have to do a form in my application with dynamic content (it comes for a web service). So, I need a scroll for the case there are many textfields and there not fit in the screen. And under of all the textfields I need a button to save the info. My problem is that if there are few textfields (that fit in the screen) the button have to appear at bottom of the screen, and not under the last textfield.
I'm using autolayout programmaticaly to add the constraints of the textfields and add them in the view one under the previous one, but I don't know what constraints I have to set to the save button.
I have uploaded two images to explain that:
http://i.stack.imgur.com/KaPp0.png
In the first image I have only two textfields, that fit in the screen, so the screen not scroll and the save button appear at the bottom of the screen.
http://i.stack.imgur.com/SqVve.png
In this second image, there are many textfields, so the screen need to scroll and the save button will be under the textfields (at 20 pixels or something like that), and not at bottom at screen.
Do you can help me? Thanks for all.
Try adding below constraint to your save button -
1) Vertical space constraint to your last added UITextfield
2) Center horizontal constraint to your content view
3) Height and width constraint
Or
1) Bottom constraint to your content view
2) Center horizontal constraint to your content view
3) Height and width constraint
4) your last added textfield bottom constraint to Save button
Related
So I tried embedding my label (that has all the text) and buttons in a scrollView to perhaps be able to scroll down to them, but when I do that, everything just disappears.
my storyboard
my preview
Just added some pictures as requested (thanks).
As for adding the scrollView (which may not be the optimal solution), I just selected all 4 buttons and the label, then selected "embed in scrollView". This led to a completely blank screen in preview.
It sounds like you need to fix the bottom constraints of your scrollview, so that the scrollview's frame does not extend past the bottom of the screen. You can then set constraints on the scrollView's content size to allow the user to scroll.
From the storyboard image you posted, it looks like there isn't a bottom constraint. Select the scrollview in the storyboard, click the pin icon, and put in a number in for the bottom constraint (e.g. 0).
I have made a Button which generates TextFields and I want a ScrollView so if I'd add too much TextFields I could scroll and write to them. I also have a Button at the bottom of my view and I want this to be visible all the time. (I don't mind if only TextFields will be inside the ScrollView or the whole view except my bottom button).
You have to set the height constraint on your TextFields. Without that, the scrollView can't figure out its contentSize. After you set the heights, the scrollView will adapt accordingly.
Also, I would use a UIView and give a fix height for it and place the TextFields inside of the UIView.
You can also use scrollView.contentSize = CGSize(width: expectedWidth, height: expectedHeight)
These steps help you to design screen according to your requirement :
Drag a ScrollView and provide height and width accordingly
Decrease height of ScrollView by height of Button which you always want to show on bottom of page
i.e- Button should be outside of ScrollView not within the ScrollView
Adjust button size and height and put it below to ScrollView
Now set Constraint to each one . First of all select ScrollView provide its l
Left , Right , Top constraint .
Select ScrollView and by pressing Control + drag mouse to Button ( below of Scrollview) and set vertical spacing between ScrollView and Button
Select Button and provide Left , Right , Bottom Constraint.
Note - As AutoLayout works differently with ScrollView so always set constraint of all control within it from Order : Top To Bottom
Hope it helps you to fix this issue . Feel free to comment if still any confusion.
Thanks.
I have a weird behavior when I use scrollView.
As you can see in the picture bellow the button called "Back" has a bottom space constraint with value 0. But we can see is not even near to the bottom of the scroll view.
I already set the scrollview to have equals width and height of the superView and top,bottom,trailing and leading space with 0 value.
How can I fix this? I want the Back button still inside of the scrollview and put it at the bottom of the view.
Update
I have the code here https://github.com/rchampa/NDParallaxIntroView and the xib is called PageB.xib
Ricardo: you've added two constraint regarding manage y position of Validate Code button where your top constraint stop to move Validate Code button towards the bottom. So please remove top constraint of Validate Code only add bottom constraint.
you need to manage all constraint like your scrollview content view height will be equal to height. Please increase some top constraint from top.
So, if I understand correctly, the problem you are running into is that your back button is unable to be positioned based on the bottom of the scroll view.
A scroll has two sets of constraints in a storyboard, one that defines the size of the scroll view, and the other that defines the content size. The top, left, bottom, right to superview constraints on the scroll view will define the size.
The part that is causing your problem is that the content size is defined by the subviews inside the scroll view. So the button cannot be placed relative to the bottom of the scroll view since the scroll view doesn't know how large its content size is. All of the subviews of a scroll view must be placed in relation to each other.
So, create constraints for your logo, text fields, and all the buttons in relation to each other. Then create constraints from the outer most subviews to the scroll view.
In your example, you would constraints from the logo to the text field, 1st text field to 2nd text field, then 2nd text flied to the label, label to the "Send email..." button, and finally "Send email..." to the "Validate Code". For the "Back" button, it would need a left align with the text fields and then a center align with the "Validate Code" button. Create a top constraint from the logo to the scroll view. Then have leading and trailing constraints from one of the text fields to the scroll view. Finally, create a bottom constraint from either the "Back" or "Validate Code" buttons to the scroll view. You will also need some alignment constraints (logo center to the text field is an example of one). After that is all setup, your view will be fully defined for the content size of the scroll view.
If I understand you, my new question is: how can I make the subviews
make the height of scroll fit 100% screen device height? Is there a
way to define weights like Android? I don't know how achieve this
since the canvas is 600*600 which is different to every device.
Response would be too long for a comment:
So you wouldn't use a scroll view for that. A scroll view is specifically for containing content that will not fit on the screen (so the user can scroll to reach the new content).
The simplest solution is to add a regular view that has TopLeftBottomRight 0 distance constraints (so it is the max height and width of the screen) and setup the view like you have here to position the bottom in the bottom left all the time.
There is also a weight system, I can explain that if you want, but it wouldn't be required thus far.
I feel like your next response will be something like "what if I want it to scroll when its too small for the current screen?". The only way I know of doing that in encapsulating all your interface into a single UIView, and changing its sized in the viewDidLoad/viewDidAppear based on the size of the scroll view. Something like this:
func viewDidLoad() {
super.viewDidLoad()
containerViewHeightConstraint.constant = scrollView.frame.size.height
containerViewWidthConstraint.constant = scrollView.frame.size.width
}
I have an image that needs a UITextField in the middle of it at all times. However, I can't get it do what I want.
I pinned the image with top, left and right constraint and gave it a height constraint. I put the UITextField in the middle of the image and tried various methods to keep it in the middle, but they all keep failing. Any tips are appreciated. Here is what the image currently looks like. First one iPhone 4 and second one is iPhone 6
You can just select the image view and the button and create two constraints: a “Horizontal Centers” constraint and a “Vertical Centers” constraint. Neither view has to be a subview of the other, and they don't have to be in the same superview either.
Here's a demo. I've already constrained the image view to fill the top left quadrant of the root view, and I've dragged in a button. In the demo I'll select the image view and the button (hold shift to select multiple views) and create the two centering constraints. Then I'll use the Preview assistant to show that the button stays centered when the image view's frame changes.
What you need to do:
Embed the UIImageView and UITextField inside a UIView.
Set constraints of UIImageView leading, trailing, top, and bottom to UIView with constant of 0.
Set constraints of UITextField center horizontally with container view, and center vertically with container view.
Then it's up to you on where you want to position that UIView. This will definitely always position the UITextField to the center of UIImageView.
I have the following screen:
If one clicks on Go Pro and buys the in app, then I'd like to remove the ads from the screen and then have both circle buttons to go to very bottom of the screen.
One possibility I thought was to have a horizontal stack view to hold the three buttons and a vertical stack view, that would first hold the horizontal stack view and then the Ads View.
Is this solution the best one or there are smarter ones?
Thanks.
UPDATE:
Here's what's happening now:
With Vertical Spacing constraint to only one button:
With Vertical Spacing constraint to both buttons
If you are using iOS 9.0 and above, you can use stackview else make outlet of height constraint of bottom view and set to 0 on close ad mob.
For the other issue i.e button stretch .. add one view that contains that 3 buttons and add constraints to that view ... just check below image and give constraints...
Now on close admob, just set constant of height constraint to 0.
and the result will be