I'm adding a UIScrollView
to a UIViewController
s view
. For some reason, between adding the scroll view to the view and it getting displayed, the contentOffset
is set to {0, -64}
, 64 being the status bar's 20 plus the navigation bar's 44 points (I guess). Below is some code that reproduces the issue, and an image.
How do I prevent iOS from setting the contentOffset
?
- (void)viewDidLoad
{
[super viewDidLoad];
_scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 100, 100, 100)];
_scroll.backgroundColor = [UIColor greenColor];
_scroll.delegate = self;
UIView *red = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
red.backgroundColor = [UIColor redColor];
[_scroll addSubview:red];
[self.view addSubview:_scroll];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// outputs {0, -64}
NSLog(@"%@", NSStringFromCGPoint(_scroll.contentOffset));
}
For disabling the same we need to make the “isScrollEnabled” property of our scroll view to false. Copy the below code in your file. import UIKit class ViewController: UIViewController { @IBOutlet var scrollView: UIScrollView! override func viewDidLoad() { super.
You cannot make a UIView scrollable. That's what UIScrollView is for. However if you are using storyboards you can try to add constraints to the view so when you rotate the device the content remains inside the viewable area. Sounds like you already have some constraints setup so I would just play around with them.
One way to do this is programmatically create an UIScrollView in your UIViewController . To control the scrollability you can set the ScrollView contentSize property.
Handling the Keyboard Appearing Notification There are two things to do in keyboardWasShown to scroll the text view. First, set the text view's content inset so the bottom edge is the keyboard's height. Second, set the scroll indicator insets to the text view's content inset.
Set automaticallyAdjustsScrollViewInsets
on your view controller to NO
, otherwise it'll adjust insets on the first subview of it's root view that happens to be of UIScrollView
class.
More on this in iOS 7 Transition Guide.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With