Swift iOS通过编程方式在导航栏下方设置scrollView约束

1qczuiv0  于 2023-01-18  发布在  iOS
关注(0)|答案(7)|浏览(168)

我的UIViewController嵌入在导航控制器中。我以编程方式添加导航按钮,现在尝试在此导航栏下方添加一个scrollView。我遇到的问题是,这会填充整个框架大小并位于导航栏下方。
如何通过编程设置这个滚动视图的约束?

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

vtwuwzda1#

Clafou的答案当然是正确的,但如果你不需要透明,想从导航栏开始,真正正确的方法是改变ViewController的行为,使其适合内容。要做到这一点,你有两个选择:
1)假设你有情节串联图板,转到视图控制器属性检查器并禁用"顶部栏下"

2)假设您通过代码完成所有操作,您将需要查找以下属性-edgesForExtendedLayoutextendedLayoutIncludesOpaqueBars。SO上已经有great answer,所以我不会在这里讨论它。
希望能有所帮助!

9lowa7mx

9lowa7mx2#

我设法让这个在iOS11上工作的唯一方法是这样的

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

beq87vna3#

雨燕5

let scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.addSubview(imageView)
scrollView.contentInsetAdjustmentBehavior = .never

NSLayoutConstraint.activate([
    scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)
])
zu0ti5jz

zu0ti5jz4#

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

kuuvgm7e

kuuvgm7e5#

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

希望这对你有用。

wgxvkvu9

wgxvkvu96#

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

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

2uluyalo7#

https://stackoverflow.com/a/75143442/851258的另一个解决方案

if let navBar = navigationController?.navigationBar {
    scrollView.contentInset = UIEdgeInsets(top: -navBar.frame.size.height, left: 0.0, bottom: 0.0, right: 0.0)
}

相关问题