简体   繁体   English

更新进度时 UIProgressView 角半径发生变化

[英]UIProgressView corner radius changing when updating progress

I have set the progressview's corner radius using layer.cornerRadius (0.2) and clipsToBounds (true).我使用 layer.cornerRadius (0.2) 和 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. UIProgressView有它自己的标准高度,并不是真的要改变。 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.每次UIProgressView使用layoutSubviews()重新加载其布局并将其设置为当前高度的 50% 时,您的自定义角半径可能会被覆盖。

I see two solutions for this problem.我看到这个问题有两种解决方案。

1. Custom Progress View (preferred) 1.自定义进度视图(首选)

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 2. 继续反击

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. UIProgressView 可能会在调用layoutSubviews()时改变其角落,因此您可以做的是尝试通过覆盖视图/单元格中的方法将其设置回您想要的值。

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.如果这还不够,请尝试不同的地方,例如prepareForReuse()layoutIfNeeded() ) 等。

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! UIProgressView不是按照您的使用方式设计的,所以我强烈建议您尝试解决方案 1并实现您自己的视图,您可以在其中设置规则:) 祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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