【问题标题】:How to get CGFloat value from NSLayoutConstraints?如何从 NSLayoutConstraints 获取 CGFloat 值?
【发布时间】:2019-04-27 19:31:17
【问题描述】:

我正在屏幕上工作,我想让这个配置文件视图循环。但我没有从imageView.frame.height 获得高度或宽度以在layer.cornerRadius 中使用它。请帮我。谢谢。

这是代码供参考。

private func addImageView() {
        topView.addSubview(profilePicView)

        NSLayoutConstraint.activate([
            profilePicView.centerXAnchor.constraint(equalTo: topView.centerXAnchor),
            profilePicView.widthAnchor.constraint(equalTo: topView.heightAnchor, multiplier: 0.3)
            ])

        profilePicViewTopAnchor = profilePicView.topAnchor.constraint(equalTo: view.topAnchor, constant: 64)
        profilePicViewTopAnchor?.isActive = true

        profilePicViewHeightAnchor = profilePicView.heightAnchor.constraint(equalTo: topView.heightAnchor, multiplier: 0.3)
        profilePicViewHeightAnchor?.isActive = true
    }

我正在尝试获取值,

override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        print((profilePicViewHeightAnchor?.constant)!)
        profilePicView.layer.cornerRadius = (profilePicViewHeightAnchor?.constant)! / 2
        profilePicView.clipsToBounds = true

    }

已解决

在大家的帮助下,我得到了这个非常适合我的解决方案,

override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        let radius = redView.frame.height * 0.3
        profilePicView.layer.cornerRadius = radius / 2
        profilePicView.clipsToBounds = true

        self.view.layoutIfNeeded()

    }

谢谢大家。非常感谢您的帮助。

【问题讨论】:

  • 你在使用 Storyboard 吗?
  • 你试过imageviewHeightConstraint.constant吗?
  • 请在问题中添加相关代码。
  • @iDev750 不,我正在以编程方式使用自动布局
  • profilePicViewHeightAnchor.constant 将始终为零。您应该在viewDidLayoutSubviews 中以imageView.frame 访问profilePicView 中的frame,并在activate 方法中移动那些topheight anchors 并删除类变量(即profilePicViewTopAnchor &profilePicViewHeightAnchor)

标签: ios swift autolayout constraints


【解决方案1】:

这一定是因为在你访问frameimageView 的时候,autolayout 没有布置布局约束。因此,如果您的 imageViewUIViewController 类中,那么您应该 override 以下方法,然后访问 widthheight

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    print(imageView.frame.height)
}

如果imageView在自定义视图中,那么你可以override下面的方法然后访问frame

override func layoutSubviews() {
    super.layoutSubviews()

    print(imageView.frame.height)
}

【讨论】:

  • 这意味着您需要为imageView 正确设置约束。如果您可以在问题中包含代码或故事板/xib 约束,这将很有帮助,以便提供更好的解决方案。
  • 更新了我的问题请看一下。
【解决方案2】:

请试试这个对你有帮助。

imageView.layer.cornerRadius =  imageView.frame.size.height / 2
imageView.clipsToBounds = true

确保 imageView 的高度和宽度相同。谢谢

【讨论】:

    【解决方案3】:

    您可以尝试在故事板中设置标识符检查器。喜欢

    【讨论】:

      【解决方案4】:

      你也可以使用ViewDidAppear。因为此时Autolayout引擎开始布局Constraint。

      override func viewDidAppear() {
          super.viewDidAppear()
          print(imageView.frame.height)
      }
      

      【讨论】:

      • 做了同样的事,但都是徒劳的
      • 所以你没有得到正确的高度?。你能在 viewdidappear 中打印其他组件的高度吗
      【解决方案5】:

      试试这个扩展:

       extension UIView {
      
              @IBInspectable
              /// Should the corner be as circle
              public var circleCorner: Bool {
                  get {
                      return min(bounds.size.height, bounds.size.width) / 2 == cornerRadius
                  }
                  set {
                      cornerRadius = newValue ? min(bounds.size.height, bounds.size.width) / 2 : cornerRadius
                  }
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-20
        • 1970-01-01
        • 1970-01-01
        • 2017-05-18
        • 1970-01-01
        • 2015-10-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多