使用圆形边框iOS

时间:2015-07-09 14:55:17

标签: ios objective-c cocoa-touch uiimageview

我正在尝试使用cornerRadius在我的UIIMageView周围创建一个完美的圆圈。我所看到的一切都说我的代码是正确的,但它会返回更多的钻石形状。 (我已导入quartzCore,不确定我需要)

image.layer.cornerRadius = (image.bounds.size.width/2);
image.layer.borderWidth = 5;
image.layer.borderColor = [UIColor whiteColor].CGColor;

有什么想法吗?

4 个答案:

答案 0 :(得分:2)

XMLHttpRequest

您的imageview的高度和宽度必须相等

答案 1 :(得分:1)

您是否尝试添加:

image.layer.masksToBounds = YES;

另一件事:你的图像宽度是否等于它的高度?

答案 2 :(得分:0)

更简单没有共同信息,请Storyboard选择UIImageView,然后在Identity Inspector下查看列,然后点击User Defined Runtime Attribute下的广告数字和关键路径:{{ 1}}或者什么,然后使用你的代码来实现,看一下屏幕:

enter image description here enter image description here

现在你的代码和:

enter image description here

如果您想使用您的代码,您可以使用同样的想法:

layer.cornerRaius Neber 150

您可以为此示例添加通用应用的功能:

//your code:

image.layer.cornerRadius = (image.bounds.size.width/2);
image.layer.borderWidth = 5;
image.layer.borderColor = [UIColor whiteColor].CGColor;

//improve with:

    CGFloat radius = 150.0f;
    self.image.layer.cornerRadius = radius;

您可以使用此方法在- (void)viewDidLoad { [super viewDidLoad]; self.image.layer.masksToBounds = YES; self.image.layer.borderWidth = 5; self.image.layer.borderColor = [UIColor whiteColor].CGColor; if (isIpnoe4 || isIphone5) { CGFloat radius = 150.0f; } else if (isIphone6) { CGFloat radius = 200.0f; } else if (isIphone6Plus) { CGFloat radius = 250.0f; } else { //here is ipad/ipadmini CGFloat radius = 250.0f; } self.image.layer.cornerRadius = radius; } 或标题中定义不同的显示:

Prefix.pch

希望这能帮到你

答案 3 :(得分:0)

步骤(1) 正如其他人所说,你需要确保你的宽度和高度是完全相同的值。如果您完全确定是这种情况,请转到步骤2.

步骤(2) 即使你认为你完全确定第1步,通常情况并非如此。我根据自己的个人经历说这话,试图做同样的事情。你会惊讶于AutoLayout在运行时对你的维度所做的一些疯狂的事情。即使你认为你的价值观是正确的,他们也会略有改变。让我猜,你是否使用Storyboard来创建你的ImageView?如果您的答案是肯定的,请转到步骤3.

步骤(3) 为了验证这个问题是由AutoLayout做一些有趣的事情造成的,所以在不使用任何Stroyboards的情况下在代码中创建一个测试UIImageView,这样做(我现在不在我的Mac上,所以语法可能有点关):

var imageView: UIImageView = UIImageView(frame: CGRect(x: 0.0, y: 0.0, width: 100.0 height: 100.0))
imageView.layer.cornerRadius = (imageView.bounds.size.width/2)
imageView.layer.borderWidth = 5.0
imageView.layer.borderColor = UIColor.redColor()
imageView.backgroundColor = UIColor.yellowColor()
self.view.addSubview(imageView)

如果你运行它并显示为正确的圆圈。那么这就是你问题的原因。我现在已经学会了用严格的代码创建我的所有对象,由于我过去遇到的所有问题,我甚至不再使用Storyboard。