【问题标题】:Swift iOS Set scrollView constraint below navigation bar programmaticallySwift iOS以编程方式在导航栏下方设置scrollView约束
【发布时间】:2015-07-15 10:56:02
【问题描述】:

我的 UIViewController 嵌入在导航控制器中。我以编程方式添加导航按钮,现在尝试在此导航栏下方添加一个滚动视图。我遇到的问题是,这是填充全帧大小并位于导航栏下方。

如何以编程方式设置此滚动视图的约束?

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)
}

【问题讨论】:

    标签: ios swift uiscrollview


    【解决方案1】:

    虽然 Clafou 的回答肯定是正确的,但如果您不需要透明度并想在导航栏下开始,那么真正正确的方法是更改​​ ViewController 的行为,使其与内容正确匹配。为此,您有两种选择:

    1) 假设您有 Storyboard,转到 ViewController Attributes Inspector 并禁用“Under top bars”

    2) 假设您通过代码实现了一切,您将需要查找以下属性 - edgesForExtendedLayoutextendedLayoutIncludesOpaqueBarsgreat answer 已经在 SO 上,所以我不会在这里介绍。

    希望对你有帮助!

    【讨论】:

    • 正是我需要的,并设法找出如何以编程方式执行此操作。谢谢
    • 不敢相信这就是全部...我遇到了一个奇怪的问题,滚动视图卡在顶部栏下方,但在显示并关闭键盘后又从下方弹出。可能存在更深层次的问题,但这个修复至少起到了作用。
    【解决方案2】:

    我设法让它在 iOS11 上运行的唯一方法就是这样

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

    【讨论】:

    • 请注意 - 这与 UIViewController 的 automaticallyAdjustsScrollViewInsets 不同。
    • 很好的答案,非常感谢。
    【解决方案3】:

    在 Swift 5 中

    let scrollView = UIScrollView()
    scrollView.translatesAutoresizingMaskIntoConstraints = false
    scrollView.addSubview(imageView)
    scrollView.contentInsetAdjustmentBehavior = .never
    
    NSLayoutConstraint.activate([
        scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)
    ])
    

    【讨论】:

    • 这将使滚动视图的内容项在导航栏下方
    【解决方案4】:

    在使用auto-layout 时,只需确保将UIScrollViewtop-constraintTop Layout Guide 一起提供,而不是与滚动视图的superview 一起提供。

    【讨论】:

    • 谢谢!你能解释一下区别吗?或者我在哪里可以看到关于这个问题的视觉文档?
    【解决方案5】:

    在 Objective-C 中,我通常将 'edgesForExtendedLayout' 属性设置为 UIRectEdgeNone

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

    不过我还是建议将约束绑定到 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)
    }
    

    希望这对你有用。

    【讨论】:

      【解决方案6】:

      要在导航栏下放置任何视图,请使用以下代码:

      yourView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-09-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-28
        相关资源
        最近更新 更多