Autolayout issue iOS 6 - ios

Newbie question.
I added UIScrollView as subview to main view in Interface Builder and after that I added UILabel above UIScrollView. In landscape mode label scrolls fine (label's height more than scrollView height) but in portrait it can't be scrolled.I realize that I need to delete some scrollView autolayout constraints, but all of them have delete option in inactive state in Utilities -> Size Inspector. How can I resolve this?

You said :
"I added UILabel above UIScrollView"
That means UILabel is a subView of UIScrollView, right?
If you are using autoLayout then this is not an issue. Using autoLayout device decides frame of the UIControls itself thats why you are not able to scroll the UILabel
Get rid of autoLayout and you will be able to scroll the UILabel after setting [scrollView setContentSize:aframe];. And if due to some reason you can not leave autoLayout then you can override autoLayout after it loads using this delegate method
- (void)viewDidLayoutSubviews
{
...........Your code goes here
}

Related

UIScrollView not scrolling with UItextfields

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.

Why is my UIView within UIScrollView not scrolling?

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

Setting up UIScrollview and autolayout

I created a UIScrollview and added a few UIElements to it, it works without autolayout no problem, scrolls etc. However its good practice to use autolayout in iOS7. Now the scrollview does not scroll. How do I accomodate for this?
If I turn auto layout off, there is extra space added to the top of the uiscrollview in ios7.
I used to set up the scrollview by 'unchecking' autolayout and implementing the following code.
//to set up the scrollview
[self.scrollView setScrollEnabled:YES];
[self.scrollView setContentSize:CGSizeMake(320, 800)];
which worked. But using autolayout prevents scrolling. Where am I going wrong?
I found the right way to do it. Put a scroll view in the view controller, set x and y 0, make height bigger than screens view, e.g 1500. Place a view in the scrollview, set its x and y to 0, set its height a little less than that of the scrollview, eg 1200. Then pin this view to the scrollview. The button for pinning is and is found in the storyboard. This worked for me like a charm. The scrollview does not need to have its coordinates set in the .m file.
This worked.

How to resize scrollView and UILabel placed on it with autolayout

I have screen in my application, which displays some multiline information.
Before autolayout, I manually set size of ScrollView and UILabel
But now, I can't do it, with autolayout, and I want to keep autolayout.
How can I solve it?
Thanks.

Cannot Scroll UIScrollView

I've got a UIScrollView in IB linked to the delegate and with the proper referencing outlet, declared in my header:
IBOutlet UIScrollView *scrollView;
And called in my viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
[scrollView setScrollEnabled:YES];
[scrollView setContentSize:CGSizeMake(320, 500)];
}
It has the correct size and width in IB, and the default values in the Attributes inspector.
Why can't I scroll it?
EDIT: Also, layer wise there is no other view conflicting for a z-index.
Is the content size larger than the scrollview's frame? That's the only way it'll scroll.
Double-check that you have content size bigger than scroll view size itself. If it will be bigger it will scroll.
m using Xcode 6/iOS 8+ now with auto-layout turned on, and I had the same problem in the first place, removing auto-layout doesn't work at all, so in order to make scroll view scrollable vertically, I made sure the following:
The scroll view content size height MUST be bigger than the screen
height, this almost goes without saying...
The top/bottom/left/right constraint of the scroll view HAS TO BE
pinned, I did this using storyboard so no code showing up here
If you want to make the scroll view scrollable horizontally, then make sure its content size width is bigger than the screen width, the other rule applies the same.
This worked for me, hope it can help someone.
enable scrolling
check "Scolling Enabled" and make sure the content size is larger than the screen height
scrollView.contentSize = CGSize(width: 375, height: <content height>)
I'm not sure what the answer to your question was when you asked.
For now, under AutoLayout, setting constraints for scroll view just solve your problem.

Resources