以编程方式添加StackView不会更改

时间:2016-08-19 09:56:41

标签: ios swift autolayout

我正在尝试以编程方式设置堆栈视图。我非常确定我已经正确设置了所有设置,但是当尝试调整简单的东西,例如堆栈视图背景或集中对齐等等时,没有任何改变,它们都在左上角对齐。

这是代码。

ShowGuideViewController:

    import UIKit
    import OAStackView

    class ShowGuideViewController: UIViewController {

        let stackView = OAStackView()

        override func viewDidLoad() {
            super.viewDidLoad()

            ShowGuideItem.all { showGuideItems in
                showGuideItems.forEach(self.viewForShowGuideItem)
            }

            stackView.axis = UILayoutConstraintAxis.Vertical
            stackView.distribution = OAStackViewDistribution.FillEqually
            stackView.alignment = OAStackViewAlignment.Center
            stackView.backgroundColor = UIColor.blueColor()

            view.addSubview(stackView)


        }

        private func viewForShowGuideItem(showGuideItem: ShowGuideItem) {
            let view = ShowGuideItemView(frame: CGRectZero)
            view.bind(showGuideItem)
            view.tapped = { _ in
                print("Open \(showGuideItem.url)")
            }
            stackView.addArrangedSubview(view)
        }


    }

显示指南项目:

    import Foundation
    import Freddy

    struct ShowGuideItem {

        let name: String
        let text: String
        let iconFileName: String
        let backgroundImageFileName: String
        let url: String

    }

    extension ShowGuideItem: JSONDecodable {

        init(json: JSON) throws {
            name = try json.string("name")
            text = try json.string("text")
            iconFileName = try json.string("iconFileName")
            backgroundImageFileName = try json.string("backgroundImageFileName")
            url = try json.string("url")
        }

    }

    extension ShowGuideItem {

        static func all(completion: ([ShowGuideItem]) -> ()) {
            let carsData = NSData(contentsOfURL: NSBundle.mainBundle().URLForResource("ShowGuide", withExtension: "json")!)!
            do {
                let json = try JSON(data: carsData)
                let items: [ShowGuideItem] = try json.arrayOf("ShowGuideItems")
                completion(items)
            } catch {
                fatalError("Failed to load Show Guide Items")
            }
        }
    }

显示指南项目视图:

    import UIKit

    class ShowGuideItemView: UIView {

        var nameLabel: UILabel!
        var textBody: UITextView!
        var iconImageView: UIImageView!
        var backGroundImageView: UIImageView!
        var tapped: (ShowGuideItemView) -> () = { _ in }
        private var touched = false

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

            setupSubviews()
        }

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

        func bind(item: ShowGuideItem) {
            nameLabel.text = item.name
            textBody.text = item.text
            iconImageView.image = UIImage(named: item.iconFileName)
            backGroundImageView.image = UIImage(named: item.backgroundImageFileName)
        }


        internal override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
            touched = true
        }

        internal override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
            if touched {
                touched = false
                tapped(self)
            }
        }

        internal override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
            touched = false
        }

        private func setupSubviews() {
            nameLabel = UILabel(frame: CGRect.zero)
            textBody = UITextView(frame: CGRect.zero)
            iconImageView = UIImageView(frame: CGRect.zero)
            backGroundImageView = UIImageView(frame: CGRect.zero)

            nameLabel.translatesAutoresizingMaskIntoConstraints = false
            textBody.translatesAutoresizingMaskIntoConstraints = false
            iconImageView.translatesAutoresizingMaskIntoConstraints = false
            backGroundImageView.translatesAutoresizingMaskIntoConstraints = false


            addSubview(nameLabel)
            addSubview(textBody)
            addSubview(iconImageView)
            addSubview(backGroundImageView)
        }
    }

你可以看到它从我记录的json文件中读取所有内容并正确输出,因为我可以看到它在构建时全部重叠。

我还没有格式化stackview中的每个项目,因为我想从堆栈视图本身居中并垂直对齐等开始。

所以,当我添加如下行:

            stackView.axis = UILayoutConstraintAxis.Vertical
            stackView.distribution = OAStackViewDistribution.FillEqually
            stackView.alignment = OAStackViewAlignment.Center
            stackView.backgroundColor = UIColor.blueColor()

to viewDidLoad没有任何变化,我不明白为什么它不会。

Current Screen Shot

1 个答案:

答案 0 :(得分:0)

viewDidLoad

中试试
stackView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(stackView)

let constraint1 = NSLayoutConstraint.constraintsWithVisualFormat(
  "V:|-[stackView]-|",
  options: [],
  metrics: nil,
  views: ["stackView": stackView])

let constraint2 = NSLayoutConstraint.constraintsWithVisualFormat(
  "H:|-[stackView]-|",
  options: [],
  metrics: nil,
  views: ["stackView": stackView])

NSLayoutConstraint.activateConstraints([constraint1, constraint2])

stackView.axis = UILayoutConstraintAxis.Vertical
stackView.distribution = OAStackViewDistribution.FillEqually
stackView.alignment = OAStackViewAlignment.Center
stackView.backgroundColor = UIColor.blueColor()