Snapkit和UILabel的轮换

时间:2017-06-12 19:23:24

标签: ios swift autolayout snapkit

我有:

  1. UIView(容器)
  2. UIView的。子视图(1) - 下图中的深蓝色
  3. UIView的。子视图(1) - 下图中的紫色
  4. 的UILabel。 edges.equalToSuperview()
  5. 我正在努力实现的目标:

    What I'm trying to accomplish

    问题是,我希望UILabel旋转3pi / 2(270°)。一旦完成旋转,它就不能正确放置。

    这是通过设置edges.equalToSuperview()和270°旋转的样子: This is how it looks with rotation and edges.equalToSuperview()

    我试过这个(但它会导致崩溃):

    myLabel.makeConstraints { make in
        make.top.equalTo(containerView.snp.left)
        make.right.equalTo(containerView.snp.top)
        make.left.equalTo(containerView.snp.bottom)
        make.bottom.equalTo(containerView.snp.right)
    }
    

    崩溃说明:

    *** Terminating app due to uncaught exception 'NSInvalidLayoutConstraintException', reason: 'Constraint improperly relates anchors of incompatible types: <SnapKit.LayoutConstraint:0x6100000ad8c0@MyClass.swift#250 MyProject.MyLabel:0x7fcc2201ca80.top == UIView:0x7fcc2201bd30.left>'
    

    我能在这做什么想法?

2 个答案:

答案 0 :(得分:1)

我使用默认自动布局完成了它,我也非常喜欢。 :)

这是功能。

func makeLabel() {
        //Creating stackview
        let stackView = UIStackView()
        view.addSubview(stackView)
        stackView.translatesAutoresizingMaskIntoConstraints = false
        stackView.alignment = .fill
        stackView.distribution = .fillEqually
        stackView.axis = .vertical

        //Creating blueView
        let blueView = UIView()
        blueView.backgroundColor = UIColor.darkGray
        blueView.translatesAutoresizingMaskIntoConstraints = false
        stackView.addArrangedSubview(blueView)
        blueView.widthAnchor.constraint(equalToConstant: 100).isActive = true

        //Creating purpleView
        let purpleView = UIView()
        purpleView.backgroundColor = UIColor.purple
        purpleView.translatesAutoresizingMaskIntoConstraints = false
        stackView.addArrangedSubview(purpleView)

        stackView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
        stackView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
        stackView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true

        //Creating rotated label
        let label = UILabel()
        view.addSubview(label)
        label.transform = CGAffineTransform.init(rotationAngle: -CGFloat.pi/2)
        label.textColor = UIColor.white
        label.text = "This is my Rotated Text"
        label.font = UIFont.systemFont(ofSize: 25)
        label.translatesAutoresizingMaskIntoConstraints = false
        label.centerXAnchor.constraint(equalTo: stackView.centerXAnchor, constant: 0).isActive = true
        label.centerYAnchor.constraint(equalTo: stackView.centerYAnchor, constant: 0).isActive = true


    }

这是输出。

<强>人像:

enter image description here

<强>风景

enter image description here

答案 1 :(得分:1)

对于任何对使用Snapkit的elk_cloner答案感兴趣的人:

myLabel.snp.makeConstraints { make in
    make.centerX.equalTo(containerView.snp.centerX)
    make.centerY.equalTo(containerView.snp.centerY)
}