Swift iOS 以编程方式在导航栏下方设置 scrollView 约束
Swift iOS Set scrollView constraint below navigation bar programmatically
我的 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)
}
在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)
}
希望这对你有用。
虽然 Clafou 的回答肯定是正确的,但如果您不需要透明度并想在导航栏下开始,真正正确的方法是更改 ViewController 的行为以使其适合内容。为此,您有两个选择:
1) 假设您有 Storyboard,转到 ViewController Attributes Inspector 并禁用 "Under top bars"
2) 假设您通过代码了解一切,您将需要查找以下属性 - edgesForExtendedLayout
和 extendedLayoutIncludesOpaqueBars
。 SO 上已经有 great answer,所以我不会在这里介绍它。
希望对您有所帮助!
在使用 auto-layout
时,请确保将 UIScrollView
的 top-constraint
与 Top Layout Guide
一起使用,而不是 superview
滚动视图。
我设法在 iOS11 上运行的唯一方法是这样的
if (@available(iOS 11.0, *)) {
scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
// Fallback on earlier versions
}
在Swift5
let scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.addSubview(imageView)
scrollView.contentInsetAdjustmentBehavior = .never
NSLayoutConstraint.activate([
scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)
])
要在导航栏下放置任何视图,请使用此代码:
yourView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true
我的 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)
}
在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)
}
希望这对你有用。
虽然 Clafou 的回答肯定是正确的,但如果您不需要透明度并想在导航栏下开始,真正正确的方法是更改 ViewController 的行为以使其适合内容。为此,您有两个选择:
1) 假设您有 Storyboard,转到 ViewController Attributes Inspector 并禁用 "Under top bars"
2) 假设您通过代码了解一切,您将需要查找以下属性 - edgesForExtendedLayout
和 extendedLayoutIncludesOpaqueBars
。 SO 上已经有 great answer,所以我不会在这里介绍它。
希望对您有所帮助!
在使用 auto-layout
时,请确保将 UIScrollView
的 top-constraint
与 Top Layout Guide
一起使用,而不是 superview
滚动视图。
我设法在 iOS11 上运行的唯一方法是这样的
if (@available(iOS 11.0, *)) {
scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
// Fallback on earlier versions
}
在Swift5
let scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.addSubview(imageView)
scrollView.contentInsetAdjustmentBehavior = .never
NSLayoutConstraint.activate([
scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)
])
要在导航栏下放置任何视图,请使用此代码:
yourView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true