Swift iOS Set scrollView constraint below navigation bar programmatically

前端 未结 5 1673
盖世英雄少女心
盖世英雄少女心 2021-01-01 15:20

My UIViewController is embedded in a navigation controller. I programmatically add the navigation buttons and now trying to add a scrollView below this navigation bar. The p

相关标签:
5条回答
  • 2021-01-01 15:41

    While Clafou's answer is certainly correct, if you don't need transparency and want to start under navigation bar, the really proper way is to change behavior of the ViewController so it fits the content properly. To do that, you have two options:

    1) Assuming you have Storyboard, go to ViewController Attributes Inspector and disable "Under top bars"

    enter image description here

    2) Assuming you are everything through code, you will want to look for following properties - edgesForExtendedLayout, and extendedLayoutIncludesOpaqueBars. There is great answer for that already on SO so I won't cover it here.

    Hope it helps!

    0 讨论(0)
  • 2021-01-01 15:41

    In Swift 5

    let scrollView = UIScrollView()
    scrollView.translatesAutoresizingMaskIntoConstraints = false
    scrollView.addSubview(imageView)
    scrollView.contentInsetAdjustmentBehavior = .never
    
    NSLayoutConstraint.activate([
        scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)
    ])
    
    0 讨论(0)
  • 2021-01-01 15:46

    The only way I managed to get this working on iOS11 was like this

    if (@available(iOS 11.0, *)) {
        scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
    }
    
    0 讨论(0)
  • 2021-01-01 15:46

    In Objective-C I usually set the 'edgesForExtendedLayout' Property to UIRectEdgeNone:

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

    Nevertheless I would recommend to bind the Constraints to the 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 讨论(0)
  • 2021-01-01 16:01

    While using auto-layout, just make sure that you give the top-constraint of UIScrollView with Top Layout Guide, not with superview of scroll view.

    0 讨论(0)
提交回复
热议问题