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.
The relationship between two user interface objects that must be satisfied by the constraint-based layout system.
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.
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.
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.
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.
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.
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.
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With