I am making a normal view where users can update their profiles. I followed these steps to make that view
Created a new UIViewController with xib file
Added a UIScrollView in super view
Added almost 9 UITextField and 1 small UIWebView
Satisfied Autolayout constraints like top UITextField will have top, left, right and height constrains and same for all the following
controls but last UITextField have top, left, right, bottom and height
constraints.
Now all the constraints are applied and satisfied but when I run the view and then try to scroll by dragging UITextField then scrollview is not scrolling but if I scroll by dragging from some area other than UITextField then it is scrolling very nice. Can anybody tell me what can be the main problem?
Note: there is no code yet other than setting up xib file.
A sample project is available on this link https://www.dropbox.com/s/7oqry8yzd9twnp1/TestScroll.zip?dl=0
Overriding UIScrollView touchesShouldCancelInContentView method will solve this problem.
According to Apple touchesShouldCancelInContentView is called before
scrolling begins if touches have already been delivered to a subview
of the scroll view. if it returns NO the touches will continue to be
delivered to the subview and scrolling will not occur.
By default this method returns NO if view is a UIControl. So the scroll doesn't happens for the UIControls.
If we returns YES from this method the touches will not be delivered to subview, So the scroll will occurs.
So override UIScrollView touchesShouldCancelInContentView like following
#interface MyScrollView : UIScrollView
#end
#implementation MyScrollView
- (BOOL)touchesShouldCancelInContentView:(UIView *)view{
return YES;
}
#end
NOTE: touchesShouldCancelInContentView method only calls if we set the canCancelContentTouches property to YES
Hope this helps.
You need to add an extra parent View for all the textfield in ScrollView.
Note:
Give constraints to UIView w.r.t. UIScrollView as Leading, Trailing, Top, Bottom and Equal Widths.
Also don't forget to add the bottom constraint for last object in UIView w.r.t. UIView (Customize it to approx. 30 since it will not
scroll much after finishing the contents).
Customize the constraint of UIView bottom w.r.t UIScrollView to 0.
See:
Set delayContentTouchesProperty to YES for UIScrollView. This would let you scroll if you touch contents of UIScrollView.
You can do it either programmatically or from storyboard.
You should use uiview as a superview on uiscrollView ,put all the textfield on uiview and make sure you have all the satisfactory constraints applied on uiview as respect of uiscrollView.
Related
How can I display a UIView under a UIScrollview with autolayout ?
I have a scrollview and a UIView below it(the grey box in image below).
Adding it has a footer inside the scrollview is not what I want because as you can see I have tabs that can scroll horizontally... The same view needs to be under each tabs. I programatically add child view's to my scroll view.
Is it possible to put a UIView outside the uiscrollview ? If not, how can I add it above every child view at the bottom ?
I have tried messing around with the constraints but nothing worked. I am stuck.
Thanks in advance !
Storyboard :
ScrollView constraints :
UIView constraints :
I found the solution. You need to drag a UIView above the UIScrollView and add a space to the top and bottom layout. Also, I added a leading and trailing space constraint.
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!
I'm trying to setup a scroll for a UIView within a UIScrollView.
My view hierarchy is:
UIView (Controller) > UIScrollView > UIView (I'll call this subView from this point on)
I have used the IB to set the layout. I set the top of the subView to be inline with the top of the UIScrollView frame. The subView is as wide as the UIScrollView frame and extends beyond the height of the UIScrollView frame.
In the subView I have UILabels (also set up using the IB). To test the scrolling, I have one label that is just peeking above the bottom border of the UIScrollView frame. When I run the app this label would ideally be able to scroll into view, however, there is no scrolling and the label is just appearing as being cut off where the UIScrollView frame ends.
In the ViewController.m I have the following:
- (void)viewDidLoad
{
self.scrollView.contentSize = self.scrollSubView.frame.size;
}
where scrollSubView is the UIView within the UIScrollView.
What am I missing (doing incorrectly) that is preventing the scroll from working?
Thanks for any assistance offered.
Possible Issue
Autolayout is on, and it won't let scrollview work as required.
Possible Solution
Check for autolayout of storyboard and untick it.
If you dont want to set it to off, you can follow this short video
which is wonderfull to deal scrolling with autolayout. :)
UIScrollView with autolayout on
I have a UIScrollView created in interface builder with a bunch of UITextView and UIImageView objects already added. I have it connected to an IBOutlet, and under -(void)viewDidLoad I set its content size to 1200x2000. User interaction Enabled, Multitouch, Scrolling Enabled and shows vertical and horizontal scroll indicators are all set to YES. When I launch the app in the iOS simulator, it doesn't scroll. What could possibly be the cause of this behavior?
viewDidLoad is to soon. You must wait until after the layout of the views has taken place. Try setting the contentSize in viewDidAppear.
Are you using autolayout? If so, check out the following answer (and forget 1.):
UIScrollView doesn't use autolayout constraints.
The best way I know how to overcome this problem without resorting to writing code is that it all can be done within a storyboard and here is how:
Leave autolayout enabled.
Make a generic UIView a subview of the UIScrollView. 2.1)Put all your View components inside this generic UIView.
Make a autolayout constraint between a subview and the UIScrollView. You must pin the right and/or bottom of the last subview to the right and/or bottom of the UIScrollView. This is how the UIScrollView knows the content size.
viewDidLoad is indeed too soon, but viewDidAppear:animated might not be late enough either. The ideal place to set the contentOffset is inside viewDidLayoutSubviews.
If you're using Autolayout, make sure you put the code to set Content size within
viewDidLayoutSubviews
I use a custom view on the UIScrollView, for pull to refresh function, and the scroll view is not scrollable. I try set contentSize with AutoLayout, and try set it by codes too. The scroll view still cannot scroll.
After spending 3 ~ 4 hours, I solve it by contentInset. It will tell scroll view to add some extra space for scrolling.
override func viewDidLayoutSubviews() {
// Add extra 15pt on the `top` of scrollView.
//self.scrollView.contentInset = UIEdgeInsetsMake(15, 0, 0, 0)
// Add extra 2pt on the `bottom` of scrollView, let it be scrollable. And the UI is more beautiful in my case.
self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 2, 0)
}
Reference docs:
Understanding the contentOffset and contentInset properties of the UIScrollView class #blog
What's the UIScrollView contentInset property for? #Stackoverflow
This is a good walk-through of scroll-views in case anyone forgot after not using them for a while: https://medium.com/#pradeep_chauhan/how-to-configure-a-uiscrollview-with-auto-layout-in-interface-builder-218dcb4022d7.
Basically, make sure you have view -> scrollview -> view, like so:
then,
1. Set scroll view constraint (top, bottom, leading and trailing) to (0,0,0,0).
2. Set inner view constraint (top, bottom, leading and trailing) to (0,0,0,0).
3. Set inner view to have equal width and equal height with parent view.
4. Select height constraint of inner view and set priority to low (250).