【问题标题】:UIStackView can I add it with autoresizing instead of autolayoutUIStackView 我可以用自动调整大小而不是自动布局来添加它吗
【发布时间】:2020-07-13 14:21:47
【问题描述】:

我尝试将 UIStackView 放置在 UIScrollView 中,并使用自动调整掩码 .flexibleWidth、.flexibleHeight -> 即水平/垂直拉伸以填充滚动视图,但如果我将其更改为带有前导/尾随/底部的自动布局代码,则堆栈视图不会出现/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

【问题讨论】:

    标签: uiscrollview autolayout uikit uistackview autoresizingmask


    【解决方案1】:

    你将toolbar添加到toolbarScroll,但你没有给它一个框架。

    toolbarScroll.addSubview(toolbar)
    
    // just to make sure...
    toolbar.translatesAutoresizingMaskIntoConstraints = true
    
    // give the toolbar stackView a frame
    toolbar.frame = toolbarScroll.bounds
    

    【讨论】:

      猜你喜欢
      • 2014-04-29
      • 2017-07-28
      • 2018-07-29
      • 1970-01-01
      • 1970-01-01
      • 2014-08-19
      • 1970-01-01
      • 2014-05-22
      • 1970-01-01
      相关资源
      最近更新 更多