使用swift第三方库Cartography实现微博九宫格

Cartography是用swift编写的autolayout开源库。
为了测试编写了一个微博九宫格的demo,以便展示Cartography的功能!
demo代码如下:


    let numForCol: Int = 3
    let spacing: CGFloat = 15
    let showView = UIView.init()
    
    func random() -> Int{// 1-9
        return Int(arc4random()%9)+1
    }
    
    func randomColor() -> UIColor{
        let red = CGFloat(arc4random_uniform(255))/CGFloat(255.0)
        let green = CGFloat(arc4random_uniform(255))/CGFloat(255.0)
        let blue = CGFloat(arc4random_uniform(255))/CGFloat(255.0)
        return UIColor.init(red:red, green:green, blue:blue , alpha: 1)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "刷新", style: UIBarButtonItemStyle.done, target: self, action: #selector(refreshShowView))
        
        showView.backgroundColor = UIColor.gray
        self.view.addSubview(showView)
        
        constrain(showView) { view in
            view.top >= topLayoutGuideCartography// 如果有navigationBar可以使用topLayoutGuideCartography属性,避开上面的导航栏,与autolayout中的topLayoutGuide功能一致
            view.centerY >= view.superview!.centerY
            view.left == view.superview!.left
            view.width == view.superview!.width
            view.height == view.width ~ 100
        }
        refreshShowView()
    }
    
    func refreshShowView(){
        
        _ = showView.subviews.map{$0.removeFromSuperview()}
        
        for index in 0..<random() {
            let label = UILabel.init()
            label.text = "\(index)"
            label.textAlignment = NSTextAlignment.center
            label.backgroundColor = randomColor()
            showView.addSubview(label)
        }
        
        constrain(showView.subviews) { array in
            for (index, viewProxy) in array.enumerated() {
                viewProxy.width == (viewProxy.superview!.width - spacing * CGFloat(numForCol + 1)) / CGFloat(numForCol)
                viewProxy.height == viewProxy.width
                
                let col : Int = index % numForCol
                let row : Int = index / numForCol
                let topEdge = row == 0 ? viewProxy.superview!.top : array[(row - 1) * numForCol + col].bottom
                let leftEdge = col == 0 ? viewProxy.superview!.left : array[row * numForCol + col - 1].right
                viewProxy.top == topEdge + spacing
                viewProxy.left == leftEdge + spacing
            }
        }
        constrain(showView,showView.subviews.last!) { showViewProxy, lastViewProxy in
            showViewProxy.bottom == lastViewProxy.bottom + spacing
        }
    }

autolayout布局图

demo地址:https://github.com/fengyunjue/jiugongge

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,602评论 4 61
  • WebSocket-Swift Starscream的使用 WebSocket 是 HTML5 一种新的协议。它实...
    香橙柚子阅读 24,378评论 8 183
  • 用汪峰的《生来彷徨》开头吧。每天走在疯狂逐梦大街上,我们精神褴褛却毫无倦意……高考一场散伙饭便把我们分到了祖国的大...
    熙兮晚归阅读 2,849评论 0 0
  • 脑子一发热把娃送进了沪上最热门的民办双语小学之一。在众人艳羡的目光中,我暗暗地自喜。之后的日子...呵呵,喜肯定是...
    凯茜的美丽世界阅读 1,594评论 0 0
  • 你是谁家好儿郎 将娶我家的美姑娘 你得 眼神温和得像棉花糖 思想纯正有担当 你是谁家好儿郎 将娶我家的美姑娘 你是...
    黛眉居阅读 3,434评论 13 9

友情链接更多精彩内容