【问题标题】:Not getting UIImageView Rounded Border while Added Multiplier for UIImageViwe为 UIImageView 添加乘数时未获得 UIImageView 圆形边框
【发布时间】:2018-08-27 06:51:23
【问题描述】:

在 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 中)吗?
  • 是的,我正在使用自动布局和约束。我只在 ViewDidLoad 中实现了这段代码。 @il3v

标签: objective-c swift uiimageview uiimage uistoryboard


【解决方案1】:

由于您的拐角半径取决于您的框架尺寸,因此您需要在框架尺寸发生变化时对其进行更新。如果您在设计中使用情节提要,当调用viewDidLoad 时,您将获得设计中的帧大小。如果不同设备的帧大小不同,您将稍后在视图layoutSubviews 或视图控制器的viewDidLayoutSubviews 中获得最终大小。

我建议的解决方案是将UIImageView 子类化并将图像视图的细节放在awakeFromNiblayoutSubviews 中,然后在适当的情况下使用此类而不是 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

【讨论】:

    【解决方案2】:

    在同样适用于我的 Viewcontroller 类中实现此代码

    -(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;
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-02
      • 2023-03-12
      相关资源
      最近更新 更多