我正在尝试获取一个UIView,其中包含一个UIStackView以等比例水平填充。
父视图是UIScrollView,而自定义UIView的框架设置为scrollView的宽度。但是,我什么也做不了,这些按钮会一直填满。他们只是收集到左边。
我已经检查了约束和堆栈视图属性。但是什么都没有。
此外,这些按钮甚至都很难使用。所以我知道我在某个地方搞砸了。
有人可以看到我在做什么错吗?
我的自定义DispatchView类调用了响应核心的XIB文件。就是上面的截图
class DispatchView: UIView {
var view: UIView!
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
loadViewFromNib()
}
override init(frame: CGRect) {
super.init(frame: frame)
loadViewFromNib()
}
func loadViewFromNib() {
let nib = UINib(nibName: String(describing: type(of: self)), bundle: Bundle(for: type(of: self)))
let view = nib.instantiate(withOwner: self, options: nil).first as! UIView
addSubview(view)
self.view = view
}
}
然后在我的ViewController
中,我叫DispatchView
。
class ViewController: UIViewController {
@IBOutlet weak var scrollView: UIScrollView!
var dispatchView: DispatchView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
dispatchView = DispatchView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 64))
dispatchView.backgroundColor = .red
scrollView.addSubview(dispatchView)
scrollView.backgroundColor = .gray
//Update scroll view content size
var contentRect = CGRect.zero
for view in scrollView.subviews {
contentRect = contentRect.union(view.frame)
}
scrollView.contentSize = CGSize(width: scrollView.frame.size.width, height: contentRect.size.height + 64)
scrollView.contentInsetAdjustmentBehavior = UIScrollView.ContentInsetAdjustmentBehavior.always
}
}