update corner radius constraint in dynamically sized cell
A view's frame
is not stored directly. It is computed from layer properties position
, bounds.size
, transform
, and anchorPoint
. What you're seeing here is that the system does not alter the frame using setFrame:
.
Instead of setting cornerRadius
in frame.didSet
, override layoutSubviews
to set it. The system schedules a call to layoutSubviews
whenever a view's size changes (and in some other circumstances).
override func layoutSubviews() {
super.layoutSubviews()
cellImage.layer.cornerRadius = (frame.height * 0.75) / 2
}
UIProgressView corner radius changing when updating progress
The UIProgressView
has it's own standard height and it's not really meant to be changed. When you set custom constraints you are probably breaking the original constraints the progress view has. Your custom corner radius is probably overridden every time the UIProgressView
reloads it's layout with layoutSubviews()
and sets it to 50% of whatever the current height is.
I see two solutions for this problem.
1. Custom Progress View (preferred)
This would be a preferred choice, simply create custom progress view and don't worry about breaking any constraints, and changing corners!
2. Keep fighting back
UIProgressView is probably changing its corners overytime layoutSubviews()
is called so what you can do is to try to set it back to your desired values by overriding the method in your view/cell.
override func layoutSubviews() {
super.layoutSubviews()
greenBar.layer.cornerRadius = 0.2
redBar.layer.cornerRadius = 0.2
}
If this is not enough, try different places, like layoutIfNeeded()
of prepareForReuse()
etc.
An alternative to using a constraints for height, you could you a transform, but then the progress bar looks kinda funny at the sides.
// x: 1 - keeps the dimention the same size
// y: 10 - makes the height 10x bigger
greenBar.transform = CGAffineTransform(scaleX: 1, y: 10)
The 2nd solution is not really a good advice since you will find yourself constantly fighting the original implementation. UIProgressView
is not designed to be used the way you are using it so I strongly suggest, try solution 1 and implement your own view, where you set the rules :) Good luck!
Very Strange View Behavior When Implementing 3D Touch Preview
It took me a while to hunt this bug down. Turns out it's caused (in my case) by https://github.com/icanzilb/EasyAnimation. When I remove the library the bug is gone.
Cheers!
Edit: As it happens it's documented here: https://github.com/icanzilb/EasyAnimation/issues/14
Corner Radius on UIToolbar?
Add
self.layer.masksToBounds = true
to the view with the corner radius.
Related Topics
Alamofireimage Disk Cache Not Working
Spritekit/Swift - How to Check Contact of Two Nodes When They Are Already in Contact
Differrence Between Closure and Function as Argument in Swift
Get Current Song Playing in Spotify on Iphone
Swift - Uipopovercontroller in iOS 8
How to Create a Rounded Rectangle Label in Xcode 7 and Swift 2
Avaudiocompressedbuffer to Uint8 Array and Vice Versa
How to Delete an Image from Photolibrary After I Pick It Up Using Uiimagepickercontroller
iOS Swift Detecting Image Rotation
Nsubiquityidentitydidchangenotification and Sigkill
Setting a Stateobject Value from Child View Causes Navigationview to Pop All Views
How to Crop a 4*3 Image in Photolibrary or Camera in Swift
Storyboard Reference to Cocoapods Storyboard Seems Broken
Today Extension with Uicollectionview Different Behaviour Compared to Single View Application
Transition Delegate for Uitabbarcontroller Animation
Infinite Scroll on iOS with Swift