UITextView inside UIScrollView with AutoLayout - objective-c

I am trying to place UITextView inside UIScrollView with AutoLayout with no luck. What I have tried is,
I placed UIScrollView inside the main view in Storyboard
I placed UITextView inside UIScrollView in Storyboard and disabled Scrolling Enabled
I set constraints (leading, trailing, top, bottom) on UIScrollView
I set constraints (top, leading, trailing, height) on UITextView
I created IBOutlet of height constraint of UITextView
I set a text (a lot of text which can cause scrolling) on UITextView in viewDidLoad()
I set a height constraint of UITextView with the code below. I have tried it right after setting text in viewDidLoad() and viewDidLayoutSubviews() with no luck
self.textViewHeightConstraint.constant = [self.textView sizeThatFits:CGSizeMake(self.textView.frame.size.width, FLT_MAX)].height;
UITextView is getting its height, but UIScrollView isn't. Is there anything I've missed?

After a few days of research and getting my hands dirty with UIScrollView + UITextView + Auto Layout, I successfully got a fully working UIScrollView. I want to share my solution just in case someone might stuck on the same situation.
Add UIScrollView inside the main view in Storyboard
Add UIView inside the UIScrollView
Add UITextView inside the UIView (the view added in step 2)
Make sure "Scrolling Enabled" of UITextView is unchecked
Add 4 constraints (leading, trailing, top, bottom) on UIScrollView
Add 4 constraints (leading, trailing, top, bottom) on UIView (the view added in step 2)
Add "Width Equally" constraint on UIView (the view added in step 2) and the main view
Add 5 constraints (leading, trailing, top, bottom, height) on UITextView. After this step you shouldn't get any errors and warnings on constraints.
Add UITextView height constraint IBOutlet on the ViewController. #property (nonatomic, weak) IBOutlet NSLayoutConstraint *textViewHeightConstraint; and connect it in Storyboard
Change the UITextView height constraint programmatically. self.textViewHeightConstraint.constant = [self.textView sizeThatFits:CGSizeMake(self.textView.frame.size.width, CGFLOAT_MAX)].height;
After all of these 10 steps, you'll get fully working UIScrollView with UITextView inside and be happy.

It seems that this question has an answer that has worked for a number of people. However, it should be noted that the documentation states:
Placing a text view inside of a scroll view. Text views handle their own scrolling. You should not embed text view objects in scroll views. If you do so, unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled.
Before trying to put a UITextView in a UIScrollView you should consider if this is really necessary. If the text view is part of a complex layout within the scroll view, then note the use of the UIView container in the accepted answer.
See also: Scroll Views Inside Scroll Views

If you noticed that the Auto Layout is still complaining about the height of the UIScrollView-
The problem here is that by default UITextView has checked the “Scrolling Enabled” in the IB.
So find that checkbox and uncheck it.

Sodbileg's solution above works, but you do not need all of his steps. I got my scrollview to scroll with a text view inside by following his steps through step 7. For step 8, I added the 4 constraints but not the height (Don't forget the bottom constraint!!). I did not do the rest.
I am not sure, but it seems that the height of the textview adjusts automatically instead of us having to manually change it.

Related

UITableView inside UIScrollview Horizontal Paging Programatically

Good day,
I have a question about programmatically adding UITableView inside UIScrollView. The structure is like that:
UIView
UIScrollView
UIView(contentView)
UIImageView
UITableView
How can I add the UITableView programmatically inside UIScrollView so when I scroll horizontally to switch between UIImageView and UITableView. Note I am using Autolayout so I need to add constraints also. If it is going to be easier with storyboard please let me know how to do it with storyboard instead of programmatically
I used SwipeView it is pretty easy to use 5 minutes installation and did what I was looking for.

UITableView null size inside UIScrollView

I had a uitableview inside a uiviewcontroller with autolayout constraints to change its size according to the main view.
Once I embedded everything inside a uiscrollview to manage the keyboard covering part of the tableview, the bottom space to superview constraint stopped working and the tableview height is always 0.
I debugged the view and that specific constraint is disabled.
Any ideas?

UITextField on UIScrollView (iOS 8.x only)

I have a view which is a subclass of UIScrollView and UITextField is on the UIScrollView. I'll refer the subclass of UIScrollView as 'CUScrollView'.
The purpose of CUScrollView is when user input a text, a button which has a title of the text will be added on the CUScrollView. The Offset of UITextField will be move at the right of the last added button.
This is how the CUScrollView look like.
Below iOS 8.x, it works perfectly; However it doesn't on iOS 8.x.
When the UITextField resigned first responder, the CUScrollView's contentSize is reset to CUScrollView's frame width.
I finally found a work around. I overrided the setContentSize: and set the contentSize to whatever I want.
Is it a bug? or some kind of auto layout mistake?
First of all don't mix auto layout with setting Frame's manually. If you perform like that then it will be the mistake in your autlayout.

Extend the edges of a UITableView under NavBar and TabBar in UIViewController in iOS 7 to get translucent effect

If I use a UItableViewController it works fine but I need to use a UItableView inside a UIViewController instead. My question is, do i have to manually program it to add extra space at the top and bottom so the first and last cells won't be partially covered and how would I move the refresh indicator down so that its not covered?
Make sure your table view is added as the first subview of your view controller. This will ensure its scroll insets are setup properly.
Also be sure to set the table view's frame to match the bounds of the view controller's view and be sure to set the autoresizingMask to flexible width and height.

IOS How to automatically rezise its subviews

I am using IOS 5. I am a very beginner in IOS. In my project i have 2 xib files. One is a UIView and other is a UIViewcotroller. The UIViewcontroller is having 3 views(TopBar,ContentArea,BottomBar). uiview also has 3 subviews. I added UIView as subview of UIViewController. My requirement is change the UIView height when i hide topbar only, bottombar only, topbar and bottombar.
Hope you can help me.
Thanks in advance.
set the views as properties,
#property (nonatomic, retain)UIView *element1_p1;
then when you need to change the size, change the frame,
self.element1_p1.frame = CGRectMake(center.x,
center.y,
elementWidth, elementHeight);
I recommend doing an animation for the change of height, so it feels better UX
great tutorial for changing views properties with animation
UIView has method as setFrame to resize the view.
Read this and this - you will get an idea how subviews respond to parent view's frame changes.