TextField overlaid on ScrollView "Adjust to Fit" TextField not working - ios

I have a scroll view overlaid by two text fields, one near the top and one near the bottom. When text in the text field reaches the side constraints the app freezes up and does not adjust the size to fit the width. Initially, I had a image view overlaid by two text fields and it was working properly. I think this has some relation to the scroll view.
In the interface builder, Adjust to Fit is checked and min font size set. I also have used the following statements and no avail.
topTextField.adjustsFontSizeToFitWidth = true
bottomTextField.adjustsFontSizeToFitWidth = true
Below is a screen shot of the app when it freezes up and screen shot of the layout in Xcode.

Looks like something to todo with Auto layout constraints. Double check constraints for scrollview .Follow this tutorial very well explained step by step how to handle constraints with scrollview.

Related

Too much text pushes my buttons below the screen

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).

iOS - weird constraints using scrollview

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
}

UITextView wrong content position with auto layout

I have a UITextView in UIViewController with auto layout:
The TextView size changes proportionally in accordance to the View (for example on rotation, or run on different dispositive iPhone 4s..6s).
Unfortunately the content text doesn't show from first line:
I suppose that the content inset is wrong but I don't have a solution.
I don't use Interface Builder - so I'm sure someone can correct me if I'm wrong, or, see the mistake if I've missed it...
But, from your screen shot (resolution is a bit low) it looks like you've got vertical constraints when positioning the text view: there's a value showing a spacing between the view and Top Layout Guide (and again for the bottom layout guide). You could just delete these...
Specifically:
Top space to equals 135
Bottom space to equals 106
As for your second screen shot, I'm not sure if it's a different device to the image on the right... but maybe the sample text just doesn't fit in the view, and it's scrolled to the bottom of the text. That would make it look like the top text is cut off, because it's clipped by the top of the UITextView.
I had the same problem, and I tried all of the work-arounds to setContentOffset and/or scrollRangeToVisible to no avail.
I was inspired by this SO post:
https://stackoverflow.com/a/21287306/610550
I ended up disabling scrolling on the textView on the storyboard, and then using self.textView.scrollEnabled = true in viewDidLoad(). It worked perfectly!

Getting rid of whitespace inside a TextView in Interface Builder

I have an issue on iOS devices larger than 4 inches.
As you can see, there is a large gap of whitespace at the bottom of my Text View. I am wondering how to clean it up to remove this whitespace using Interface Builder.
Simply change your bottom constraint to >= current value instead of equal and the UITextView will start resizing provided scrolling enabled is unchecked.
You just need to uncheck
Scrolling Enabled
Got the answers from here
Auto - Resizing
If you are using the auto-resizing for the text view then remove set the following setting for the auto-resizing tab.
This might be your settings, for the text view
Change it to the following setting,
Do not stretch the content of the text view,
And if you are worried about the content not displaying in the set height then that is not an issue, there is a already a scroll in the text view controller, make sure that is enabled.
Auto - Layout
If You are using Auto Layout in your app then set the constraints as shown in the below image on your text view,
Add these four constraints,
Left, right, top and the height of the text view.

UIScrollView auto layout not working the width of contentView

i'm back developing IOS after a year and a half. I'm really having difficulty in make use of UIScrollView and auto layout.
I'm developing an application running just in Iphone and portrait orientation.
I have a simple form inside my content view...
label
textField
textField
textField
textField
button
textField
view
imageView
label
The problem is with the width of the contentView's content. The size is been defined for the labels. I would like (And really have to do) o size it according with the screen size.
Here is my constraints of my first text field:
Here is my simulator screen running the app:
how I mentioned before, it seems to get the size according the label and not according the screen size. If I create a constraint of size for the first label, this size change but I would like to get the size dinamically based on screen.
I didn't write any code, I want to do it preferentially using auto layout to understand the concepts of auto layout better.
If someone could help me to solve it... I would be very grateful.
Pin your contentView.width equal to scrollview.width.
Add a constraint to your scroll view's content view child to be the same width as the root-level view.

Resources