One, foreword

Recently we have been able to abandon iOS8 for iOS adaptation. Even the latest version of wechat requires iOS11. So we can use the controls after iOS9 with enthusiasm. For example: UIStackView

UIStackView inherits from UIView and is used to manage views within it. It can greatly improve the efficiency of interface development. Every iOS developer should be skilled in using UIStackView.

Second, the effect of

1, UIStackView + UIScrollow effect

2, UIStackView alone effect

Iii. Project Description

The project code is relatively easy to understand, and the layout uses SnapKit.

Enumerated type

Since we are sharing panels, enumerations are essential types, and we define enumerations for sharing platforms, as well as enumerations for sharing styles

Public enum LGSharePlateformEnum: String {case wechat = "wechat" case wechatTimeline = "circle of friends" case qqFriend = "QQ" case qqZone = "QQ space "case dingTalk =" dingTalk" case none = "" public func info() -> (title: String? , icon: UIImage?) {switch self {case. Wechat: return (" wechat ", UIImage(named: "nav_share_weixin")) case.qqfriend: Return ("QQ", UIImage(named: "nav_share_qq")) case.wechatTimeline: return (" circle of friends ", UIImage(named: "nav_share_qq")) "Nav_share_pengyouquan ") case. QqZone: return ("QQ space ", UIImage(named:" nav_share_kaliao_QQzone ") case. Case. None: return (nil, nil)}}}Copy the code
Public enum LGShareTypeEnum {case Normal case scrollow}Copy the code

1, because we use Swift development, enumeration can define string type enumeration, and even call methods, greatly facilitate our development efficiency

We define an enumeration of.None, due to the nature of UIStackView, complete with empty elements.

A single line of sliding sharing panels

public class LGShareScrollView: UIView { var didSelectItem: ((_ plateform: LGSharePlateformEnum?) -> Void)? // Platform share lazy var platformStackView: UIStackView = { let stackView = UIStackView() stackView.spacing = 25 stackView.isUserInteractionEnabled = true stackView.axis = .horizontal stackView.distribution = .equalSpacing return stackView }() var plateform: [LGSharePlateformEnum]? { didSet { for plateItem in plateform ?? [] { let itemView = LGShareItemView() itemView.didSelectItem = didSelectItem itemView.sharePlateform = plateItem platformStackView.addArrangedSubview(itemView) itemView.snp.makeConstraints { (make) in make.height.equalTo(100) make.width.equalTo(50) } } } } private var scrollow: UIScrollView? public var safeBottomHeight: CGFloat { var bottomH: CGFloat = 0.0 if #available(iOS 11.0, *) { bottomH = UIApplication.shared.delegate?.window??.safeAreaInsets.bottom ?? 0 } return bottomH } override init(frame: CGRect) { super.init(frame: frame) setupStackView() } override public func layoutSubviews() { super.layoutSubviews() let stackViewWidth = platformStackView.frame.size.width scrollow?.contentSize = CGSize(width: stackViewWidth + 30, height: 0) } func setupStackView() { self.isUserInteractionEnabled = true let scrollow = UIScrollView() scrollow.showsHorizontalScrollIndicator = false self.scrollow = scrollow scrollow.isUserInteractionEnabled = true self.addSubview(scrollow) scrollow.snp.makeConstraints { (make) in make.top.left.right.equalToSuperview() make.height.equalTo(100) make.bottom.equalToSuperview().offset(-safeBottomHeight) } scrollow.addSubview(platformStackView) platformStackView.snp.makeConstraints { (make) in make.top.bottom.equalToSuperview() make.left.equalToSuperview().offset(15) make.height.equalTo(100) } } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } }Copy the code

1. Use UIScrollView + UIStackView

2. Set the top, left, bottom and height of UIStackView. SnapKit AutoLayout will automatically calculate the width

3. Set scrollow.contentSize in layoutSubviews, otherwise it will not slide

Auto-wrap sharing panel

public class LGShareStackView: UIView {

    var didSelectItem: ((_ plateform: LGSharePlateformEnum?) -> Void)?

    var lineNumber = 5
    //纵向的
    lazy var platformStackView: UIStackView = {
        let stackView = UIStackView()
        stackView.isUserInteractionEnabled = true
        stackView.axis = .vertical
        stackView.distribution = .fillEqually
        return stackView
    }()

    var plateform: [LGSharePlateformEnum]? {
        didSet {

            let line = (((plateform?.count ?? 0) - 1) / lineNumber) + 1
            for i in 0..<line {
                let stackView = UIStackView()
                stackView.isUserInteractionEnabled = true
                stackView.axis = .horizontal
                stackView.distribution = .fillEqually
                platformStackView.addArrangedSubview(stackView)
                stackView.snp.makeConstraints { (make) in
                    make.left.right.equalToSuperview()
                    make.height.equalTo(100)
                }
                for j in 0..<lineNumber {
                    let plateItem = (((i * lineNumber) + j) < plateform?.count ?? 0) ? plateform?[((i * lineNumber) + j)] : LGSharePlateformEnum.none
                    let itemView = LGShareItemView()
                    itemView.didSelectItem = didSelectItem
                    itemView.sharePlateform = plateItem
                    stackView.addArrangedSubview(itemView)
                    itemView.snp.makeConstraints { (make) in
                        make.height.equalTo(100)
                        make.top.equalToSuperview()
                    }
                }
            }

        }
    }

    public var safeBottomHeight: CGFloat {
        var bottomH: CGFloat = 0.0
        if #available(iOS 11.0, *) {
            bottomH = UIApplication.shared.delegate?.window??.safeAreaInsets.bottom ?? 0
        }
        return bottomH
    }


    override init(frame: CGRect) {
        super.init(frame: frame)
        setupStackView()

    }

    func setupStackView() {
        self.isUserInteractionEnabled = true
        self.addSubview(platformStackView)
        platformStackView.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(-safeBottomHeight)
        }
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
Copy the code

1, logic is a vertical UIStackView, set a number of horizontal UIStackView 2, can set the number of lines to set the number of each line

As is shown in

Four, the last

IOS9 after the use of UIStackView, do not consider the views coordinates, similar to android linear layout. Very comfortable and convenient to use. Github address: github.com/Qinzhao/LGS…