简体   繁体   中英

How to get CGFloat value from NSLayoutConstraints?

I am working on screen where I want to make this profile view circular. But I am not getting height or width from imageView.frame.height to use it in layer.cornerRadius . Please help me. Thanks.

在此输入图像描述

Here is the code for reference.

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
    }

And I am trying to get values as,

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

    }

SOLVED

After everyones help I got this solution which works perfectly fine for me,

override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

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

        self.view.layoutIfNeeded()

    }

Thanks to all you guys. Really appreciated the help.

This must be because the time you are accessing the frame for imageView , the layout constraints are not laid out by the autolayout . So, If your imageView is inside a UIViewController class then you should override the below method and then access the width and height .

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    print(imageView.frame.height)
}

If imageView is inside a custom view then you can override the below method and then access the frame ,

override func layoutSubviews() {
    super.layoutSubviews()

    print(imageView.frame.height)
}

Please try this it helps to you.

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

Make sure your height and width of imageView is same. Thanks

You can try to set in storyboard on identifier inspector. like

在此输入图像描述

you can Also use ViewDidAppear.because at this time Autolayout engine started to layout Constraint.

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

Try this extension:

 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
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM