Swift iOS 以编程方式在导航栏下方设置滚动视图约束
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31428483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift iOS Set scrollView constraint below navigation bar programmatically
提问by Jiri Trecak
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 problem I'm having is this is filling the full frame size and going under the navigation bar.
我的 UIViewController 嵌入在导航控制器中。我以编程方式添加导航按钮,现在尝试在此导航栏下方添加滚动视图。我遇到的问题是填充整个框架大小并进入导航栏下方。
How do I programmatically set constraints of this 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)
}
回答by Jiri Trecak
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:
虽然 Clafou 的答案当然是正确的,但如果您不需要透明度并希望在导航栏下开始,真正正确的方法是更改 ViewController 的行为,使其适合内容。为此,您有两个选择:
1) Assuming you have Storyboard, go to ViewController Attributes Inspector and disable "Under top bars"
1) 假设您有 Storyboard,请转到 ViewController Attributes Inspector 并禁用“Under top bars”
2) Assuming you are everything through code, you will want to look for following properties - edgesForExtendedLayout
, and extendedLayoutIncludesOpaqueBars
. There is great answerfor that already on SO so I won't cover it here.
2) 假设您通过代码了解一切,您将需要查找以下属性 - edgesForExtendedLayout
、 和extendedLayoutIncludesOpaqueBars
。SO上已经有很好的答案,所以我不会在这里介绍。
Hope it helps!
希望能帮助到你!
回答by Zayin Krige
The only way I managed to get this working on iOS11 was like this
我设法让它在 iOS11 上运行的唯一方法是这样的
if (@available(iOS 11.0, *)) {
scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
// Fallback on earlier versions
}
回答by Ravi Sisodia
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.
使用 时auto-layout
,请确保您提供的top-constraint
是UIScrollView
with Top Layout Guide
,而不是superview
of 滚动视图。
回答by damo
In Swift 5
在斯威夫特 5
let scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.addSubview(imageView)
scrollView.contentInsetAdjustmentBehavior = .never
NSLayoutConstraint.activate([
scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)
])
回答by cr0ss
In Objective-C I usually set the 'edgesForExtendedLayout' Property to UIRectEdgeNone:
在 Objective-C 中,我通常将 'edgesForExtendedLayout' 属性设置为UIRectEdgeNone:
if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
self.edgesForExtendedLayout = UIRectEdgeNone;
Nevertheless I would recommend to bind the Constraints to the topLayoutGuide
尽管如此,我还是建议将约束绑定到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.
希望这对你有用。