2017-06-12 57 views
0

我:Snapkit和UILabel的旋轉

  1. 的UIView(容器)
  2. 的UIView。 (1)的子視圖 - 下圖中的深藍色
  3. UIView。 (1)的子視圖 - 下圖中的紫色
  4. UILabel。 edges.equalToSuperview()

我試圖做到:

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:[email protected]#250 MyProject.MyLabel:0x7fcc2201ca80.top == UIView:0x7fcc2201bd30.left>' 

任何想法,我可以在這裏做什麼?

回答

1

有興趣的人elk_cloner的回答是:

myLabel.snp.makeConstraints { make in 
    make.centerX.equalTo(containerView.snp.centerX) 
    make.centerY.equalTo(containerView.snp.centerY) 
} 
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

景觀使用Snapkit

enter image description here

+0

非常感謝! –