我尝试将UIStackView放置在具有自动调整大小的蒙版.flexibleWidth,.flexibleHeight->的UIScrollView内,即水平/垂直拉伸以填充滚动视图,但是如果将其更改为使用前导/尾随/底部/自动布局的代码,则不会出现堆栈视图顶部约束,然后视图正确显示
toolbar.autoresizingMask = [.flexibleWidth, .flexibleHeight]
toolbar.backgroundColor = .clear
toolbar.axis = .horizontal
toolbar.distribution = .fill
toolbar.alignment = .fill
toolbar.spacing = 8
toolbarScroll.frame = bounds
toolbarScroll.autoresizingMask = [.flexibleHeight, .flexibleWidth]
toolbarScroll.showsHorizontalScrollIndicator = false
toolbarScroll.showsVerticalScrollIndicator = false
toolbarScroll.backgroundColor = UIColor.green
toolbarScroll.addSubview(toolbar)
这里是在addSubview之后添加的代码,然后工具栏显示在toolbarScroll中
toolbar.translatesAutoresizingMaskIntoConstraints = false
toolbar.leadingAnchor.constraint(equalTo: toolbarScroll.leadingAnchor).isActive = true
toolbar.trailingAnchor.constraint(equalTo: toolbarScroll.trailingAnchor).isActive = true
toolbar.bottomAnchor.constraint(equalTo: toolbarScroll.bottomAnchor).isActive = true
toolbar.topAnchor.constraint(equalTo: toolbarScroll.topAnchor).isActive = true
您在toolbar
上添加了toolbarScroll
,但没有给它一个框。
toolbarScroll.addSubview(toolbar)
// just to make sure...
toolbar.translatesAutoresizingMaskIntoConstraints = true
// give the toolbar stackView a frame
toolbar.frame = toolbarScroll.bounds