問題描述
在將視圖添加到我的應用程序之前,我創建了一個小型演示游樂場以使其正常工作.
我有一個滾動視圖,其中包含許多用于水平滾動的按鈕.我知道這些按鈕需要進入滾動視圖中的容器視圖,并且也創建了它.我最初使用自動布局約束來創建所有這些,但現在嘗試使用常量來確保內容視圖大于滾動視圖.但是,按鈕仍然不會滾動......我錯過了什么嗎?滾動視圖是否不適用于自動布局?
我也在我的 iPad 上以編程方式完成這一切,所以很遺憾,界面生成器的解決方案不是一個選項......
這里是完整的代碼:
導入 UIKit導入游樂場支持類FilterViewController:UIViewController {var filterView: UIView!var 滾動視圖:UIScrollView!var 容器視圖:UIView!覆蓋 func loadView() {過濾視圖 = UIView()視圖 = 過濾視圖view.backgroundColor = #colorLiteral(紅色:0.909803926944733,綠色:0.47843137383461,藍色:0.643137276172638,alpha:1.0)滾動視圖 = UIScrollView()scrollView.backgroundColor = #colorLiteral(紅色:0.474509805440903,綠色:0.839215695858002,藍色:0.976470589637756,alpha:1.0)view.addSubview(scrollView)scrollView.translatesAutoresizingMaskIntoConstraints = falsescrollView.topAnchor.constraint(equalTo:view.topAnchor, constant:40).isActive = truescrollView.leadingAnchor.constraint(equalTo:view.leadingAnchor).isActive = truescrollView.widthAnchor.constraint(equalTo:view.widthAnchor).isActive = truescrollView.heightAnchor.constraint(equalToConstant: 200).isActive = truescrollView.isScrollEnabled = true容器視圖 = UIView()containerView.backgroundColor = #colorLiteral(紅色:0.176470592617989,綠色:0.498039215803146,藍色:0.756862759590149,alpha:1.0)scrollView.addSubview(containerView)containerView.frame = CGRect(x: 0, y: 0, width: 1080, height: 200)}類按鈕{讓按鈕 = UIButton()初始化(標題文本:字符串){button.backgroundColor = #colorLiteral(紅色:0.976470589637756,綠色:0.850980401039124,藍色:0.549019634723663,alpha:1.0)button.setTitle(titleText, for: .normal)button.frame = CGRect(x: 0, y: 0, 寬度: 200, 高度: 200)}}覆蓋 func viewDidLoad() {super.viewDidLoad()讓 b1 = Buttons(titleText: "one")讓 b2 = Buttons(titleText: "two")讓 b3 = Buttons(titleText: "三")讓 b4 = Buttons(titleText: "four")讓 b5 = Buttons(titleText: "五")讓 buttonArray = [b1,b2,b3,b4,b5]var startPoint : CGFloat = 0.0對于按鈕數組中的 btn {讓 theBtn = btn.buttoncontainerView.addSubview(theBtn)theBtn.frame = CGRect(x: startPoint, y: 0, width: 200, height: 200)起點 += 220}}}讓 filterViewController = FilterViewController()PlaygroundPage.current.liveView = filterViewController
謝謝你!這是包含所有自動布局約束的完整(正在運行)迷你項目:
導入 UIKit導入游樂場支持類FilterViewController:UIViewController {var filterView: UIView!var 滾動視圖:UIScrollView!var 容器視圖:UIView!覆蓋 func loadView() {過濾視圖 = UIView()視圖 = 過濾視圖view.backgroundColor = #colorLiteral(紅色:0.909803926944733,綠色:0.47843137383461,藍色:0.643137276172638,alpha:1.0)滾動視圖 = UIScrollView()scrollView.backgroundColor = #colorLiteral(紅色:0.474509805440903,綠色:0.839215695858002,藍色:0.976470589637756,alpha:1.0)view.addSubview(scrollView)scrollView.translatesAutoresizingMaskIntoConstraints = falsescrollView.topAnchor.constraint(equalTo:view.topAnchor, constant:40).isActive = truescrollView.leadingAnchor.constraint(equalTo:view.leadingAnchor).isActive = truescrollView.widthAnchor.constraint(equalTo:view.widthAnchor).isActive = truescrollView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.25).isActive = truescrollView.isScrollEnabled = true容器視圖 = UIView()containerView.backgroundColor = #colorLiteral(紅色:0.176470592617989,綠色:0.498039215803146,藍色:0.756862759590149,alpha:1.0)scrollView.addSubview(containerView)containerView.translatesAutoresizingMaskIntoConstraints = falsecontainerView.topAnchor.constraint(equalTo:scrollView.topAnchor).isActive = truecontainerView.leadingAnchor.constraint(equalTo:scrollView.leadingAnchor).isActive = truecontainerView.trailingAnchor.constraint(equalTo:scrollView.trailingAnchor).isActive = truecontainerView.bottomAnchor.constraint(equalTo:scrollView.bottomAnchor).isActive = truecontainerView.heightAnchor.constraint(equalTo: scrollView.heightAnchor).isActive = true}類按鈕{讓按鈕 = UIButton()初始化(標題文本:字符串){button.backgroundColor = #colorLiteral(紅色:0.976470589637756,綠色:0.850980401039124,藍色:0.549019634723663,alpha:1.0)button.setTitle(titleText, for: .normal)//button.frame = CGRect(x: 0, y: 0, width: 200, height: 200)}}覆蓋 func viewDidLoad() {super.viewDidLoad()讓 b1 = Buttons(titleText: "one")讓 b2 = Buttons(titleText: "two")讓 b3 = Buttons(titleText: "三")讓 b4 = Buttons(titleText: "four")讓 b5 = Buttons(titleText: "五")讓 buttonArray = [b1,b2,b3,b4,b5]var startPoint = containerView.leadingAnchor對于按鈕數組中的 btn {讓 theBtn = btn.buttoncontainerView.addSubview(theBtn)theBtn.translatesAutoresizingMaskIntoConstraints = falsetheBtn.leadingAnchor.constraint(equalTo:startPoint, constant:20).isActive = truetheBtn.topAnchor.constraint(equalTo:containerView.topAnchor).isActive = truetheBtn.bottomAnchor.constraint(equalTo:containerView.bottomAnchor).isActive = truetheBtn.widthAnchor.constraint(equalTo: theBtn.heightAnchor).isActive = truestartPoint = theBtn.trailingAnchorcontainerView.widthAnchor.constraint(equalTo: theBtn.widthAnchor, multiplier:CGFloat(buttonArray.count), constant: CGFloat(buttonArray.count * 20)).isActive = true}}}讓 filterViewController = FilterViewController()PlaygroundPage.current.liveView = filterViewController
您可以使用 Auto Layout 來做到這一點.secret 是將 containerView
的邊緣約束到 scrollView
的邊緣.這不直觀,但限制 containerView
的邊緣并沒有設置大小,它只是確保 scrollView
的內容大小隨著 containerView 的增長而增長
增長.通過將 containerView
的寬度約束設置為大于 scrollView
寬度的常數,內容將水平滾動.
注意:這樣配置scrollView
時,不要設置scrollView
的contentSize
.contentSize
將由 Auto Layout 為您計算,它將等于 containerView
的大小.確保 containerView
的大小完全由約束指定非常重要.
為了讓它發揮作用,我做了以下更改:
containerView = UIView()containerView.backgroundColor = #colorLiteral(紅色:0.176470592617989,綠色:0.498039215803146,藍色:0.756862759590149,alpha:1.0)scrollView.addSubview(containerView)//containerView.frame = CGRect(x: 0, y: 0, width: 1080, height: 200)containerView.translatesAutoresizingMaskIntoConstraints = falsecontainerView.topAnchor.constraint(equalTo:scrollView.topAnchor).isActive = truecontainerView.leadingAnchor.constraint(equalTo:scrollView.leadingAnchor).isActive = truecontainerView.trailingAnchor.constraint(equalTo:scrollView.trailingAnchor).isActive = truecontainerView.bottomAnchor.constraint(equalTo:scrollView.bottomAnchor).isActive = truecontainerView.heightAnchor.constraint(equalToConstant: 200).isActive = truecontainerView.widthAnchor.constraint(equalToConstant: 1080).isActive = true
<小時><塊引用>
為什么我的內容不滾動?
要使其滾動,containerView
必須大于scrollView
.您的錯誤是您設置了約束,使得 containerView
的寬度和高度與 scrollView
相同,這就是您的內容不滾動的原因.
如果你想讓它水平滾動,containerView
的寬度必須大于scrollView
的寬度.您可以通過以下兩種方式之一執行此操作:
為
containerView
指定一個顯式的常量寬度,該寬度大于scrollView
的寬度.或
將
containerView
的子視圖從左到右連接起來,最左邊的被限制在containerView
的前沿.完全指定子視圖的寬度,并在子視圖之間放置距離約束.最右邊的子視圖必須與containerView
的后沿有一個偏移量.通過這樣做,Auto Layout 可以計算containerView
的寬度并設置scrollView
的contentSize
.
<小時>
小項目:更新
這是您的迷你項目的一個版本,它使用一系列受約束的視圖來定義 containerView
的寬度.關鍵是 viewDidLoad()
中 for
循環之后的最終約束,它將最后一個按鈕的 trailingAnchor(又名 startPoint
)連接到 containerView
的 trailingAnchor.這樣就完成了將 containerView
的前沿與 containerView
的后沿連接起來的約束和按鈕鏈.有了這個,Auto Layout 能夠計算出 containerView
的寬度,并確定 scrollView
的 contentSize
.
導入 UIKit導入游樂場支持類FilterViewController:UIViewController {var filterView: UIView!var 滾動視圖:UIScrollView!var 容器視圖:UIView!覆蓋 func loadView() {過濾視圖 = UIView()視圖 = 過濾視圖view.backgroundColor = #colorLiteral(紅色:0.909803926944733,綠色:0.47843137383461,藍色:0.643137276172638,alpha:1.0)滾動視圖 = UIScrollView()scrollView.backgroundColor = #colorLiteral(紅色:0.474509805440903,綠色:0.839215695858002,藍色:0.976470589637756,alpha:1.0)view.addSubview(scrollView)scrollView.translatesAutoresizingMaskIntoConstraints = falsescrollView.topAnchor.constraint(equalTo: view.topAnchor, 常量: 40).isActive = truescrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = truescrollView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = truescrollView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.25).isActive = truescrollView.isScrollEnabled = true容器視圖 = UIView()containerView.backgroundColor = #colorLiteral(紅色:0.176470592617989,綠色:0.498039215803146,藍色:0.756862759590149,alpha:1.0)scrollView.addSubview(containerView)containerView.translatesAutoresizingMaskIntoConstraints = false//這是關鍵:將 containerView 的所有四個邊連接到//到scrollView的邊緣containerView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = truecontainerView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = truecontainerView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor).isActive = truecontainerView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true//使 containerView 和 scrollView 高度相同意味著//內容不會垂直滾動containerView.heightAnchor.constraint(equalTo: scrollView.heightAnchor).isActive = true}類按鈕 {讓按鈕 = UIButton()初始化(標題文本:字符串){button.backgroundColor = #colorLiteral(紅色:0.976470589637756,綠色:0.850980401039124,藍色:0.549019634723663,alpha:1.0)button.setTitle(titleText, for: .normal)}}覆蓋 func viewDidLoad() {super.viewDidLoad()讓 b1 = Buttons(titleText: "one")讓 b2 = Buttons(titleText: "two")讓 b3 = Buttons(titleText: "三")讓 b4 = Buttons(titleText: "four")讓 b5 = Buttons(titleText: "五")讓 buttonArray = [b1, b2, b3, b4, b5]var startPoint = containerView.leadingAnchor對于按鈕數組中的 btn {讓 theBtn = btn.buttoncontainerView.addSubview(theBtn)theBtn.translatesAutoresizingMaskIntoConstraints = falsetheBtn.leadingAnchor.constraint(equalTo: startPoint, constant: 20).isActive = truetheBtn.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = truetheBtn.bottomAnchor.constraint(equalTo: containerView.bottomAnchor).isActive = truetheBtn.widthAnchor.constraint(equalTo: theBtn.heightAnchor).isActive = truestartPoint = theBtn.trailingAnchor}//完成約束鏈containerView.trailingAnchor.constraint(equalTo: startPoint, constant: 20).isActive = true}}讓 filterViewController = FilterViewController()PlaygroundPage.current.liveView = filterViewController
I have created a small demo playground to get this working before adding the view to my app.
I have a scroll view that is going to contain a number of buttons to scroll through horizontally. I know that these buttons need to go into a container view within the scroll view and have created this as well. I was initially using autolayout constraints to create all of this, but have now tried using constants to ensure the content view is bigger than the scroll view. However, the buttons still will not scroll... have I missed something? Do scroll views not work with auto layout?
I am doing this all programmatically as well on my iPad so solutions with interface builder are unfortunately not an option...
Here is the full code:
import UIKit
import PlaygroundSupport
class FilterViewController: UIViewController {
var filterView: UIView!
var scrollView: UIScrollView!
var containerView: UIView!
override func loadView() {
filterView = UIView()
view = filterView
view.backgroundColor = #colorLiteral(red: 0.909803926944733, green: 0.47843137383461, blue: 0.643137276172638, alpha: 1.0)
scrollView = UIScrollView()
scrollView.backgroundColor = #colorLiteral(red: 0.474509805440903, green: 0.839215695858002, blue: 0.976470589637756, alpha: 1.0)
view.addSubview(scrollView)
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.topAnchor.constraint(equalTo:view.topAnchor, constant:40).isActive = true
scrollView.leadingAnchor.constraint(equalTo:view.leadingAnchor).isActive = true
scrollView.widthAnchor.constraint(equalTo:view.widthAnchor).isActive = true
scrollView.heightAnchor.constraint(equalToConstant: 200).isActive = true
scrollView.isScrollEnabled = true
containerView = UIView()
containerView.backgroundColor = #colorLiteral(red: 0.176470592617989, green: 0.498039215803146, blue: 0.756862759590149, alpha: 1.0)
scrollView.addSubview(containerView)
containerView.frame = CGRect(x: 0, y: 0, width: 1080, height: 200)
}
class Buttons{
let button = UIButton()
init (titleText : String){
button.backgroundColor = #colorLiteral(red: 0.976470589637756, green: 0.850980401039124, blue: 0.549019634723663, alpha: 1.0)
button.setTitle(titleText, for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
}
}
override func viewDidLoad() {
super.viewDidLoad()
let b1 = Buttons(titleText: "one")
let b2 = Buttons(titleText: "two")
let b3 = Buttons(titleText: "three")
let b4 = Buttons(titleText: "four")
let b5 = Buttons(titleText: "five")
let buttonArray = [b1,b2,b3,b4,b5]
var startPoint : CGFloat = 0.0
for btn in buttonArray {
let theBtn = btn.button
containerView.addSubview(theBtn)
theBtn.frame = CGRect(x: startPoint, y: 0, width: 200, height: 200)
startPoint += 220
}
}
}
let filterViewController = FilterViewController()
PlaygroundPage.current.liveView = filterViewController
Thank you vacawama! Here is the full (working now) mini project with all of the auto layout constraints:
import UIKit
import PlaygroundSupport
class FilterViewController: UIViewController {
var filterView: UIView!
var scrollView: UIScrollView!
var containerView: UIView!
override func loadView() {
filterView = UIView()
view = filterView
view.backgroundColor = #colorLiteral(red: 0.909803926944733, green: 0.47843137383461, blue: 0.643137276172638, alpha: 1.0)
scrollView = UIScrollView()
scrollView.backgroundColor = #colorLiteral(red: 0.474509805440903, green: 0.839215695858002, blue: 0.976470589637756, alpha: 1.0)
view.addSubview(scrollView)
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.topAnchor.constraint(equalTo:view.topAnchor, constant:40).isActive = true
scrollView.leadingAnchor.constraint(equalTo:view.leadingAnchor).isActive = true
scrollView.widthAnchor.constraint(equalTo:view.widthAnchor).isActive = true
scrollView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.25).isActive = true
scrollView.isScrollEnabled = true
containerView = UIView()
containerView.backgroundColor = #colorLiteral(red: 0.176470592617989, green: 0.498039215803146, blue: 0.756862759590149, alpha: 1.0)
scrollView.addSubview(containerView)
containerView.translatesAutoresizingMaskIntoConstraints = false
containerView.topAnchor.constraint(equalTo:scrollView.topAnchor).isActive = true
containerView.leadingAnchor.constraint(equalTo:scrollView.leadingAnchor).isActive = true
containerView.trailingAnchor.constraint(equalTo:scrollView.trailingAnchor).isActive = true
containerView.bottomAnchor.constraint(equalTo:scrollView.bottomAnchor).isActive = true
containerView.heightAnchor.constraint(equalTo: scrollView.heightAnchor).isActive = true
}
class Buttons{
let button = UIButton()
init (titleText : String){
button.backgroundColor = #colorLiteral(red: 0.976470589637756, green: 0.850980401039124, blue: 0.549019634723663, alpha: 1.0)
button.setTitle(titleText, for: .normal)
//button.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
}
}
override func viewDidLoad() {
super.viewDidLoad()
let b1 = Buttons(titleText: "one")
let b2 = Buttons(titleText: "two")
let b3 = Buttons(titleText: "three")
let b4 = Buttons(titleText: "four")
let b5 = Buttons(titleText: "five")
let buttonArray = [b1,b2,b3,b4,b5]
var startPoint = containerView.leadingAnchor
for btn in buttonArray {
let theBtn = btn.button
containerView.addSubview(theBtn)
theBtn.translatesAutoresizingMaskIntoConstraints = false
theBtn.leadingAnchor.constraint(equalTo:startPoint, constant:20).isActive = true
theBtn.topAnchor.constraint(equalTo:containerView.topAnchor).isActive = true
theBtn.bottomAnchor.constraint(equalTo:containerView.bottomAnchor).isActive = true
theBtn.widthAnchor.constraint(equalTo: theBtn.heightAnchor).isActive = true
startPoint = theBtn.trailingAnchor
containerView.widthAnchor.constraint(equalTo: theBtn.widthAnchor, multiplier:CGFloat(buttonArray.count), constant: CGFloat(buttonArray.count * 20)).isActive = true
}
}
}
let filterViewController = FilterViewController()
PlaygroundPage.current.liveView = filterViewController
You can do this with Auto Layout. The secret is to constrain the edges of the containerView
to the edges of the scrollView
. It's not intuitive, but constraining the edges of the containerView
doesn't set the size, it just makes sure that the content size of the scrollView
grows as the containerView
grows. By setting constraints for the width of the containerView
to a constant that is a larger number than the width of the scrollView
, the content will scroll horizontally.
Note: When configuring a scrollView
this way, you do not set the contentSize
of the scrollView
. The contentSize
will be computed for you by Auto Layout and it will be equal to the size of the containerView
. It is important to make sure that the size of the containerView
is fully specified by the constraints.
Here's what I changed to make it work:
containerView = UIView()
containerView.backgroundColor = #colorLiteral(red: 0.176470592617989, green: 0.498039215803146, blue: 0.756862759590149, alpha: 1.0)
scrollView.addSubview(containerView)
//containerView.frame = CGRect(x: 0, y: 0, width: 1080, height: 200)
containerView.translatesAutoresizingMaskIntoConstraints = false
containerView.topAnchor.constraint(equalTo:scrollView.topAnchor).isActive = true
containerView.leadingAnchor.constraint(equalTo:scrollView.leadingAnchor).isActive = true
containerView.trailingAnchor.constraint(equalTo:scrollView.trailingAnchor).isActive = true
containerView.bottomAnchor.constraint(equalTo:scrollView.bottomAnchor).isActive = true
containerView.heightAnchor.constraint(equalToConstant: 200).isActive = true
containerView.widthAnchor.constraint(equalToConstant: 1080).isActive = true
Why isn't my content scrolling?
For it to scroll, the containerView
must be larger than the scrollView
. Your error is that you have set the constraints such that the containerView
is the same width and height as the scrollView
, and that is why your content isn't scrolling.
If you want it to scroll horizontally, the width of the containerView
must be larger than the scrollView
's width. You can do this in one of two ways:
Specify an explicit constant width for the
containerView
that is larger than thescrollView
's width.OR
Chain the subviews of the
containerView
from left to right with the left most being constained to the leading edge of thecontainerView
. Fully specify the widths of the subviews, and place distance contraints between the subviews. The rightmost subview must have an offset from the trailing edge of thecontainerView
. By doing this, Auto Layout can compute the width of thecontainerView
and set thecontentSize
of thescrollView
.
Mini project: update
This is a version of your mini project which uses a chain of constrained views to define the containerView
's width. The key is the final constraint after the for
loop in viewDidLoad()
which connects the last button's trailingAnchor (aka startPoint
) to the containerView
's trailingAnchor. This completes the chain of contraints and buttons which connect the leading edge of the containerView
with the trailing edge of containerView
. With this, Auto Layout is able to compute the width of the containerView
and establish the contentSize
of the scrollView
.
import UIKit
import PlaygroundSupport
class FilterViewController: UIViewController {
var filterView: UIView!
var scrollView: UIScrollView!
var containerView: UIView!
override func loadView() {
filterView = UIView()
view = filterView
view.backgroundColor = #colorLiteral(red: 0.909803926944733, green: 0.47843137383461, blue: 0.643137276172638, alpha: 1.0)
scrollView = UIScrollView()
scrollView.backgroundColor = #colorLiteral(red: 0.474509805440903, green: 0.839215695858002, blue: 0.976470589637756, alpha: 1.0)
view.addSubview(scrollView)
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.topAnchor.constraint(equalTo: view.topAnchor, constant: 40).isActive = true
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
scrollView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
scrollView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.25).isActive = true
scrollView.isScrollEnabled = true
containerView = UIView()
containerView.backgroundColor = #colorLiteral(red: 0.176470592617989, green: 0.498039215803146, blue: 0.756862759590149, alpha: 1.0)
scrollView.addSubview(containerView)
containerView.translatesAutoresizingMaskIntoConstraints = false
// This is key: connect all four edges of the containerView to
// to the edges of the scrollView
containerView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
containerView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = true
containerView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor).isActive = true
containerView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
// Making containerView and scrollView the same height means the
// content will not scroll vertically
containerView.heightAnchor.constraint(equalTo: scrollView.heightAnchor).isActive = true
}
class Buttons {
let button = UIButton()
init(titleText: String) {
button.backgroundColor = #colorLiteral(red: 0.976470589637756, green: 0.850980401039124, blue: 0.549019634723663, alpha: 1.0)
button.setTitle(titleText, for: .normal)
}
}
override func viewDidLoad() {
super.viewDidLoad()
let b1 = Buttons(titleText: "one")
let b2 = Buttons(titleText: "two")
let b3 = Buttons(titleText: "three")
let b4 = Buttons(titleText: "four")
let b5 = Buttons(titleText: "five")
let buttonArray = [b1, b2, b3, b4, b5]
var startPoint = containerView.leadingAnchor
for btn in buttonArray {
let theBtn = btn.button
containerView.addSubview(theBtn)
theBtn.translatesAutoresizingMaskIntoConstraints = false
theBtn.leadingAnchor.constraint(equalTo: startPoint, constant: 20).isActive = true
theBtn.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
theBtn.bottomAnchor.constraint(equalTo: containerView.bottomAnchor).isActive = true
theBtn.widthAnchor.constraint(equalTo: theBtn.heightAnchor).isActive = true
startPoint = theBtn.trailingAnchor
}
// Complete the chain of constraints
containerView.trailingAnchor.constraint(equalTo: startPoint, constant: 20).isActive = true
}
}
let filterViewController = FilterViewController()
PlaygroundPage.current.liveView = filterViewController
這篇關于我的帶有自動布局約束的 Swift 4 UIScrollView 沒有滾動的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!