Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

enter image description here

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.

like image 358
Vijay Kharage Avatar asked Nov 26 '18 07:11

Vijay Kharage


People also ask

What is NSLayout constraint?

The relationship between two user interface objects that must be satisfied by the constraint-based layout system.

What is multiplier in Swift?

Multiplier is there for creating Proportional Constraint. Auto Layout calculates the first item's attribute to be the product of the second item's attribute and this multiplier . Any value other than 1 creates a proportional constraint.

What is auto Layout in IOS?

Auto Layout defines your user interface using a series of constraints. Constraints typically represent a relationship between two views. Auto Layout then calculates the size and location of each view based on these constraints. This produces layouts that dynamically respond to both internal and external changes.

What is a nslayoutconstraint?

A relationship between two layout attributes used in a constraint-based layout. An NSLayoutConstraint specifies the relationship between two layout attributes ( FirstAttribute and SecondAttribute, both of which are of type NSLayoutAttribute) in a constraint-based layout. A floating point Priority.

What is a layout constraint?

The layout constraint orientation, either horizontal or vertical, that the constraint uses to enforce layout between objects. Keys that specify a horizontal or vertical layout constraint between objects.

What are the advantages of combining multiple constraints in a layout?

By combining multiple constraints, you can define layouts that dynamically adapt as the size and location of the elements in your user interface change. For some example layouts, see Stack Views in Auto Layout Guide.

What is the first attribute of a constraint?

The attribute of the first item participating in the constraint. The first item participating in the constraint. Handle (pointer) to the unmanaged object representation. Applied to the second attribute participating in the constraint.


1 Answers

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)
}
like image 150
Kamran Avatar answered Oct 18 '22 06:10

Kamran