我可以使用自动调整大小而不是自动布局来添加UIStackView

时间:2020-04-01 18:12:25

标签: uiscrollview autolayout uikit uistackview autoresizingmask

我尝试将UIStackView放置在具有自动调整大小的掩码.flexibleWidth,.flexibleHeight->的UIScrollView内,即水平/垂直拉伸以填充滚动视图,但是如果将其更改为带有前导/尾随/底部的自动布局代码,则不会出现堆栈视图/ top约束,然后视图正确显示

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

1 个答案:

答案 0 :(得分:0)

您在toolbar上添加了toolbarScroll,但没有给它一个框架。

toolbarScroll.addSubview(toolbar)

// just to make sure...
toolbar.translatesAutoresizingMaskIntoConstraints = true

// give the toolbar stackView a frame
toolbar.frame = toolbarScroll.bounds
相关问题