Swift iOS Sets the scroll bar restriction below the navigation bar programmatically

So, my UIViewController is built into the navigation controller. I am adding navigation buttons programmatically and am now trying to add scrollView under this navigation bar. The problem I encountered is filling out the full frame size and going under the navigation bar.

How can I programmatically set the limits of this scroll?

var scrollView: UIScrollView! var containerView = UIView() override func viewDidLoad() { super.viewDidLoad() self.navigationItem.title = "Filters" // add some buttons on the navigation self.scrollView = UIScrollView() self.scrollView.backgroundColor = UIColor.grayColor() self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height) containerView = UIView() scrollView.addSubview(containerView) view.addSubview(scrollView) let label = UILabel(frame: CGRectMake(0, 0, 100, 21)) label.text = "my label" containerView.addSubview(label) } 
+8
ios swift uiscrollview
source share
5 answers

While Clafou's answer is certainly correct if you don't need transparency and want to start with the navigation bar, the really right way is to change the ViewController's behavior to fit the content correctly. You have two options for this:

1) Assuming you have a storyboard, go to ViewController Attributes Inspector and turn off "Under top bars"

enter image description here

2) Assuming you are all through code, you need to look for the following properties - edgesForExtendedLayout and extendedLayoutIncludesOpaqueBars . There is an excellent answer for this already on SO, so I will not cover it here.

Hope this helps!

+39
source share

Use the contentInset property of your UIScrollView . For example, if you want to break 44 points at the top:

 self.scrollView.contentInset = UIEdgeInsets(top: 44, left: 0, bottom: 0, right: 0) 
+2
source share

When using auto-layout just make sure you specify the top-constraint of UIScrollView using the Top Layout Guide and not with the superview scroll.

+2
source share

In Objective-C, I usually set the "edgeForExtendedLayout" property to UIRectEdgeNone:

 if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) self.edgesForExtendedLayout = UIRectEdgeNone; 

However, I would recommend binding restrictions to topLayoutGuide

 func addScrollViewConstraints() { var scrollViewContraints = [NSLayoutConstraint]() scrollViewContraints.append(NSLayoutConstraint(item: scrollView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.topLayoutGuide, attribute:NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0.0)) scrollViewContraints.append(NSLayoutConstraint(item: scrollView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.bottomLayoutGuide, attribute:NSLayoutAttribute.Top, multiplier: 1.0, constant: 0.0)) scrollViewContraints.append(NSLayoutConstraint(item: scrollView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute:NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0.0)) scrollViewContraints.append(NSLayoutConstraint(item: scrollView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute:NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0.0)) self.view.addConstraints(scrollViewContraints) } 

Hope this works for you.

0
source share

The only way I managed to work on iOS11 was this:

 if (@available(iOS 11.0, *)) { scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } else { // Fallback on earlier versions } 
0
source share

All Articles