Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Snapkit和UILabel';s旋转_Ios_Swift_Autolayout_Snapkit - Fatal编程技术网

Ios Snapkit和UILabel';s旋转

Ios Snapkit和UILabel';s旋转,ios,swift,autolayout,snapkit,Ios,Swift,Autolayout,Snapkit,我有: UIView(容器) 尤维尤。(1)的子视图-下图中为深蓝色 尤维尤。(1)的子视图-下图中的紫色 UILabel。edges.equalToSuperview() 我正在努力实现的目标: 问题是,我希望UILabel旋转3pi/2(270°)。一旦我完成了旋转,它就没有正确放置 这是通过设置edges.equalToSuperview()和270°旋转的效果: 我尝试过这个(但它导致崩溃): 事故描述: *** Terminating app due to uncaught exc

我有:

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

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

    这是通过设置edges.equalToSuperview()和270°旋转的效果:

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

    事故描述:

    *** 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>'
    
    ***由于未捕获异常“NSInvalidLayoutConstraintException”而终止应用程序,原因:“约束与不兼容类型的锚不正确关联:”
    

    你知道我可以在这里做什么吗?

    我已经用默认的自动布局完成了,我也很喜欢。:)

    这是函数。

    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
    
    
        }
    
    这是输出。

    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
    
    
        }
    
    肖像:

    景观


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

    这是函数。

    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
    
    
        }
    
    这是输出。

    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
    
    
        }
    
    肖像:

    景观


    对于任何对麋鹿克隆人使用Snapkit的答案感兴趣的人:

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

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

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

    非常感谢你!非常感谢你!