如何创建自己舍入的UIImageView扩展(How to create an UIImageView extension that is rounded by itself)

编程入门 行业动态 更新时间:2024-10-27 10:30:05
如何创建自己舍入的UIImageView扩展(How to create an UIImageView extension that is rounded by itself)

我最近遇到了一个恼人的问题。

假设我想在我的应用程序中的多个位置使用图像,并且在任何地方都必须将其舍入为圆形 。 因为我希望我的代码干净,我认为这个功能(舍入本身)应该放在扩展UIImageView的类中。 我创建了一个简单的类来覆盖storyboard构造函数,并负责正确设置cornerRadius:

class RoundedImageView: UIImageView { required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) self.layer.cornerRadius = self.frame.size.width/2 self.layer.borderColor = UIColor.redColor().CGColor self.layer.borderWidth = 2 self.layer.masksToBounds = true } }

对于类似的约束

宽度:100,

身高:100

这显然有效,因为在调用构造函数后框架不会更改(因为它在storyboard中具有正确的尺寸)。 例如,当约束看起来像这样时,就会出现问题:

宽度等于0.5 superview的

纵横比:1:1

因为在调用构造函数后框架会发生变化。

我最终做的是这样的:

override func viewDidLayoutSubviews(){ super.viewDidLayoutSubviews() myRoundedImageInstance.layer.cornerRadius = ... }

在每个视图控制器中都有这个类的实例......这是有效的,因为在imageView改变维度后重新计算框架,但这很难看在这种情况下似乎没有类的存在。

我尝试将帧的重新计算放在drawRect中 - 也不起作用。 有任何想法吗?

I've been experiencing an annoying problem recently.

Assume I want to use an image in multiple places across my application, and everywhere it has to be rounded so as to be a circle. Because I want my code to be clean, I think that this functionality (of rounding itself) should be put inside the class extending UIImageView. I have created a simple class that overrides storyboard constructor and is responsible for setting cornerRadius properly:

class RoundedImageView: UIImageView { required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) self.layer.cornerRadius = self.frame.size.width/2 self.layer.borderColor = UIColor.redColor().CGColor self.layer.borderWidth = 2 self.layer.masksToBounds = true } }

For constraints like

width : 100,

height : 100

this obviously works, as the frame is not changed after the constructor is called (because it has correct dimensions in storyboard). The problem occurs when, for example, the constraints look like this:

width equals to 0.5 superview's

aspect ratio: 1:1

because the frame changes after the constructor is called.

What I ended up doing is something like this:

override func viewDidLayoutSubviews(){ super.viewDidLayoutSubviews() myRoundedImageInstance.layer.cornerRadius = ... }

in every view controller that has the instance of this class inside... ...which works, because the frame is recalculated after the imageView changes dimensions but this is ugly, and existence of the class seems to be unnecessary in this case.

I tried putting frame's recalculation in drawRect - doesn't work either. Any ideas?

最满意答案

您可以将圈子更新代码移动到layoutSubviews方法中。

You could move your circle updating code into your layoutSubviews method.

更多推荐

本文发布于:2023-07-24 01:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1240178.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:create   UIImageView   rounded   extension

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!