美文网首页
iOS StackView子视图显示比例调整

iOS StackView子视图显示比例调整

作者: SoaringHeart | 来源:发表于2019-11-26 17:27 被阅读0次
    //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)
    

    相关文章

      网友评论

          本文标题:iOS StackView子视图显示比例调整

          本文链接:https://www.haomeiwen.com/subject/lqdywctx.html