iOS StackView子视图显示比例调整

2019-11-26  本文已影响0人  SoaringHeart
//The stack view does not know the size of the text view because it is scrollable by default. You need to disable scrolling:
textView.isScrollEnabled = false
【注意点】
stackView.distribution = .fillProportionally
UIStackView 中方向属性为:.axis; 
NSStackView 中方向属性为:.orientation; 

----------------- iOS -----------------

@available(iOS 9.0, *)
@objc public extension UIStackView {
    
    /// 设置子视图显示比例(此方法前请设置 .axis/.orientation)
    func setSubViewMultiplier(_ multiplier: CGFloat, at index: Int) {
        if index < subviews.count {
            let element = subviews[index];
            if self.axis == .horizontal {
                element.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: multiplier).isActive = true

            } else {
                element.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: multiplier).isActive = true
            }
        }
    }
}
🌰:
//内部视图水平排列
stackView.axis = .vertical
stackView.setSubViewMultiplier(0.2, at: 0)

----------------- macApp -----------------

@available(OSX 10.9, *)
@objc public extension NSStackView {

    /// 设置子视图显示比例(此方法前请设置 .axis/.orientation)
    func setSubViewMultiplier(_ multiplier: CGFloat, at index: Int) {
        if index < subviews.count {
            let element = subviews[index];
            if self.orientation == .horizontal {
                element.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: multiplier).isActive = true

            } else {
                element.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: multiplier).isActive = true
            }
        }
    }
}

🌰:
//内部视图水平排列
stackView.orientation = .horizontal      
stackView.setSubViewMultiplier(0.2, at: 0)
上一篇下一篇

猜你喜欢

热点阅读