为 UIImageViwe 添加乘数时未获得 UIImageView 圆角边框
Not getting UIImageView Rounded Border while Added Multiplier for UIImageViwe
在 Storyboard 中,我将 Multiplier 用于 UIImageView 的高度和宽度,然后我只想要圆形边框,所以我使用了下面的代码,它不适用于所有 iPhone。
_profileImgView.clipsToBounds = YES;
_profileImgView.layer.backgroundColor = color.CGColor;
_profileImgView.layer.cornerRadius =_profileImgView.frame.size.width/2;
_profileImgView.layer.borderColor = [UIColor colorWithRed:253.0/255.0 green:182.0/255.0 blue:43.0/255.0 alpha:100].CGColor;
_profileImgView.layer.borderWidth = 5.0f;
由于您的角半径取决于您的帧大小,因此您需要在帧大小发生变化时更新它。如果您在设计中使用故事板,当调用 viewDidLoad
时,您将获得设计 中 的帧大小。如果不同设备的帧大小不同,您将在稍后的时间点在视图 layoutSubviews
或可能是视图控制器的 viewDidLayoutSubviews
.
中获得最终大小
我建议的解决方案是子 class UIImageView
并将图像视图的细节放在 awakeFromNib
和 layoutSubviews
中,然后使用这个 class 在适当的地方代替 UIImageView。
// CircularImageView.h
#import <UIKit/UIKit.h>
@interface CircularImageView : UIImageView
@end
// CircularImageView.m
@implementation CircularImageView
- (void)awakeFromNib {
[super awakeFromNib];
self.clipsToBounds = YES;
self.layer.borderColor = [UIColor colorWithRed:253.0/255.0 green:182.0/255.0 blue:43.0/255.0 alpha:100].CGColor;
self.layer.borderWidth = 5.0f;
}
- (void)layoutSubviews {
[super layoutSubviews];
self.layer.cornerRadius = self.frame.size.width / 2;
}
@end
在同一个 Viewcontroller 中实现此代码 class 它对我有用
-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
_profileImgView.clipsToBounds = YES;
_profileImgView.layer.backgroundColor = color.CGColor;
_profileImgView.layer.cornerRadius =_profileImgView.frame.size.width/2;
_profileImgView.layer.borderColor = [UIColor colorWithRed:253.0/255.0 green:182.0/255.0 blue:43.0/255.0 alpha:100].CGColor;
_profileImgView.layer.borderWidth = 5.0f;
}
在 Storyboard 中,我将 Multiplier 用于 UIImageView 的高度和宽度,然后我只想要圆形边框,所以我使用了下面的代码,它不适用于所有 iPhone。
_profileImgView.clipsToBounds = YES;
_profileImgView.layer.backgroundColor = color.CGColor;
_profileImgView.layer.cornerRadius =_profileImgView.frame.size.width/2;
_profileImgView.layer.borderColor = [UIColor colorWithRed:253.0/255.0 green:182.0/255.0 blue:43.0/255.0 alpha:100].CGColor;
_profileImgView.layer.borderWidth = 5.0f;
由于您的角半径取决于您的帧大小,因此您需要在帧大小发生变化时更新它。如果您在设计中使用故事板,当调用 viewDidLoad
时,您将获得设计 中 的帧大小。如果不同设备的帧大小不同,您将在稍后的时间点在视图 layoutSubviews
或可能是视图控制器的 viewDidLayoutSubviews
.
我建议的解决方案是子 class UIImageView
并将图像视图的细节放在 awakeFromNib
和 layoutSubviews
中,然后使用这个 class 在适当的地方代替 UIImageView。
// CircularImageView.h
#import <UIKit/UIKit.h>
@interface CircularImageView : UIImageView
@end
// CircularImageView.m
@implementation CircularImageView
- (void)awakeFromNib {
[super awakeFromNib];
self.clipsToBounds = YES;
self.layer.borderColor = [UIColor colorWithRed:253.0/255.0 green:182.0/255.0 blue:43.0/255.0 alpha:100].CGColor;
self.layer.borderWidth = 5.0f;
}
- (void)layoutSubviews {
[super layoutSubviews];
self.layer.cornerRadius = self.frame.size.width / 2;
}
@end
在同一个 Viewcontroller 中实现此代码 class 它对我有用
-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
_profileImgView.clipsToBounds = YES;
_profileImgView.layer.backgroundColor = color.CGColor;
_profileImgView.layer.cornerRadius =_profileImgView.frame.size.width/2;
_profileImgView.layer.borderColor = [UIColor colorWithRed:253.0/255.0 green:182.0/255.0 blue:43.0/255.0 alpha:100].CGColor;
_profileImgView.layer.borderWidth = 5.0f;
}