简体   繁体   中英

UIProgressView corner radius changing when updating progress

I have set the progressview's corner radius using layer.cornerRadius (0.2) and clipsToBounds (true). However, when I use an alert to update the progress, the corner radius changes. If I close and reopen the app, the corner radius is correct again.

Does anyone have any idea what is happening here? Any thoughts would be really helpful, thanks!

在此处输入图像描述

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!

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