My UIViewController contains a UITableView (is always on top, the same width with the superview), and a UITextView (is always under the UITableView 10.0f), as below:
Now I want to use autolayout to raise up the UITextView when the keyboard appears. I add the following constraints for UITableView (sticks to the top, and is always on top of UITextView 10.0f):
UITableView : Width (320), Height (706), Left, Right, Top, Bottom (10.0f)
UITextView : Width (280), Height (44), Left, Right
I assign IBOutlet the UITableView's height constraint, and modify its value in code. For example, when the keyboard appears, the height of UITableView will be decreased -216.0f (the height of the keyboard), and as a result, the UITextView will be raised up on top of the keyboard (because the UITextView is always under the UITableView 10.0f).
So, that's what I expected. However, for the only first step, when running application, the UITableView takes up the whole screen, and I do not see my UITextView. Is there any problem with my constraints?
the keyboards default behaviour is to cover the screen, autolayout cant account for it since its not part of your layout originally, i wrote a category that will provide this functionality for you here, just #import it into your viewController and it should just work
Related
I have a form on an XCode Project using UITableView with Swift, where the first section of the form are UITextView in a UITableViewCell, and on the last cell is a UITextView with a height of 300, and constraints of 0 to the top, bottom, right, and left of the UITableViewCell. The form is bigger than the screen height. If you type on a UITextField without scrolling to the bottom and show the UITextView before, the UITextView goes missing and leaves a gap. (Screehshot attached below). I am using static cells, so I'm not calling the TableView functions.
Any help is appreciated. Thanks :)
EDIT: Screenshots of dummy form. It has a navigationController with large titles, which makes the notes section not show on screen when loaded
I changed the size of the UITableViewCell to fit the UITextArea, and remove the height constraint to the UITextArea
Can't build content inside scroll view with autolayout. Yes, I've investigated a lot, read apple technical note and some articles, I know, that content view should be constrained with scroll view's superview but can't still achieve the desired result. I expect this result:
fixed or scrollable (for small screens) portrait mode
always scrollable landscape mode, not to loose or overlap my fixed content
my button (red view) should always be at the bottom
my photo (yellow view) should always be fixed size and not to loose content
(like in my wrong landscape mode)
Making this work is actually pretty easy. All you have to do are the following steps:
Add the UIScrollView as subview of your UIViewController`
Add the UIView as a subview of the UIScrollView
Add the UIButton as a subview of the UIViewController. Do not add it as a subview of the UIScrollView if you want it to stick to the bottom.
Then add the following constraints:
Pin the UIScrollView to the top, left and right of you UIViewControllers view
Pin the UIButtonto the left, right and bottom of your UIViewControllers view
Pin the UIButton top to the UIScrollView bottom
Pin the UIImageView top, horizontalCenter and bottom to the UIScrollView
Set the UIImageView height and width constraints
Set the UIButton height constraint
And that's it. Do not add anything to the UIScrollView contentView and do not set the contentSize programmatically! The contentSize ist set automatically by Auto Layout.
Here are screenshots of the constraints and the result:
I making a article detail view like Facebook.
This view have content(profile image, writer, date, content text) and comment write view.
So Top view is UIScrollview, Bottom view is UIView.
UIScrollView have UIImageview, UILabel*3,
UIView have UITextView, UIButton.
When touch UITextView in UIView, the keyboard comes up.
So UIScrollview will be smaller and UIView position will be moved.
And User will write text, UITextView line will be added so UIView Size will be extended.
Eventually UIScrollview and UIView height not Determinate.
How to set autolayout this view?
Instead of having UIView for textView and button, what i would suggest you is, just add one scrollView set its constraints as below
Then add a UIView as container view inside scroll view, (select scroll view go to Editor, select Embed In, and select View) and set its constraint as below
Now add your imageView, labels, textView and button one by one, and make sure your set imageView's top space to view and button's (which is at the bottom of screen) bottom space to view, and all other view's top space and bottom space to their nearest views, this way scroll view can easily determine the height required for it.
One last thing, when user click inside textView you might need to move scrollView upward in order to make textView visible to user while he is typing, to do this one easy solution is just download the library 'TPKeyboardAvoiding' from here add all classes to your project and once done, set your scrollView's custom class to 'TPKeyboardAvoidingScrollView'. Clean and run the project, it should work..
May try it:
What I say below is about constraint:
UIScrollView top, left, right equal self.view top, left, right
UIScrollView bottom equal UIView bottom
UIView left, right, bottom equal self.view left, right, bottom UIView height equal 200(you set it)
OK, when you UITextView is touched, set the UIView height constraint to what you want(maybe 300)
I think it works!
So I have a UIViewController with a bunch of IBOutlet's elements placed on a UIScrollView(ContentView). All are using Auto Layout and are working perfect except for the UITextView. Due to its position I cannot figure out what constraints to add to the bottom of the UITextView so the scroll view will notice it's full size depending on the text. Currently the UITextView is underneath/off the view.
This is how the view is composed.
Can't figure out the last constraint for the UITextview
The constraints on the UITextView are as follows:
Trailing Space to: Superview
(Equals: 8)
Leading Space to: SuperView
(Equals: 9)
Top Space to: MapView
(Equals: 70)
These constraints are exactly the way I want the UITextView positioned. It's just the bottom constraint I can't figure out so that the content size is of UIScrollView big enough for the UITextView.
If there's anymore information I need to provide please let me know. I've been stuck with this the past few days.
First make sure "Scrolling Enabled" of your UITextView is disable then
add five constraints(leading, trailing, top, bottom, height) on UITextView and add an IBOutlet on the view controller for the UITextView height constraint. Finally just change the UITextView height constraint programmatically.
I have a layout as follows(Using AutoLayout,iPad):
The UIScrollView has Top,Bottom,Leading,Trailing space to the superview as 0. The UIView also has all four similar constraints. The second UILabel has its height set to 0 programmatically (since more than a screenful of text is shown here). Also, these four pin constraints have also been set up from the UIView to UIScrollView.
When the XIB is loaded, this UILabel shows long lines of text correctly. But scrolling does not work. This is a screenshot of the running app on an iPad:
The yellow background is set for the outermost view and the grey one for the UIView. If you see carefully at the end of the screenshot, the UILabel's text seems to overflow past the UIView.
I suspect that because the UIView cannot "capture" the size of it's subviews, the UIScrollView cannot calculate it's contentsize too(there's no reason for the scrollbar to work if its content view is only equal to one screen). Why is this happening even when the UILabel is clearly a subview of the UIView? If my suspicions are right, how do I set the UIView's height equal to the actual content size so that my UILabel is properly "contained" in it?
(Setting the UIView's height,width statically is not an option) .
NB: Tested the XIB without the UIView and only a single UILabel (with long text) inside a UIScrollView. Now scrolling works(since, I presume, the scroll view can calculate its content size).
NB2: I also tried the answers in this SO question and this post but my UIView simply wont extend beyond the dimensions of the screen. (I only care about horizontal scrolling)
AutoLayout issues are very hard to explain with text. I was having the same problem. I found the solution in this video. hope this will help you too.
https://www.youtube.com/watch?v=UnQsFlMGDsI
This video demonstrates how to create a UIScrollView which has some
views on the top of the screen and a view on the bottom of the screen
in Autolayout. UIScrollView content size is automatically adjusted to
the size of the screen or to the size of the content in such way that
on smaller devices scrolling is active and on larger devices scrolling
is not active because it is not needed. UIButton below UITextFields
always stays on the bottom of the screen and everything works on all
iPhone resolutions (iPhone 4 / 5 / 6 / 6 Plus).