所以我的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)
}
答案 0 :(得分:7)
我设法让这个在iOS11上运行的唯一方法是这样的
if (@available(iOS 11.0, *)) {
scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
// Fallback on earlier versions
}
答案 1 :(得分:4)
使用auto-layout
时,只需确保top-constraint
UIScrollView
Top Layout Guide
superview
,而不是<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_centerInParent="true" />
</RelativeLayout>
滚动视图。
答案 2 :(得分:2)
在Swift 5中
let scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.addSubview(imageView)
scrollView.contentInsetAdjustmentBehavior = .never
NSLayoutConstraint.activate([
scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)
])
答案 3 :(得分:1)
使用contentInset
的{{1}}属性。例如,如果你想在顶部有44个点的差距:
UIScrollView