iOS:UIButton在iOS

时间:2016-08-12 09:28:53

标签: ios objective-c iphone uibutton

我需要在UIButton外画一个像边框一样的圆圈。下面是附加图像和代码。我还需要在按钮下方显示文字。以下代码将在其下添加图像和文本。但是我如何拥有图层。enter image description here

   UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [imageButton setBackgroundImage:image forState:UIControlStateNormal];
    imageButton.translatesAutoresizingMaskIntoConstraints = NO;
    imageButton.backgroundColor = [UIColor clearColor];
    [imageButton setTitle:title forState:UIControlStateNormal];
    imageButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    imageButton.titleLabel.font = [UIFont fontWithName:@"OpenSans" size:fon];
    [imageButton setTitleEdgeInsets:UIEdgeInsetsMake(95, 0.0f, 0.0f, 0.0f)];
    [imageButton setTitleColor:[UIColor iconTextColor] forState:UIControlStateNormal];
    [imageButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

2 个答案:

答案 0 :(得分:1)

设置按钮宽度和高度ex:width = 100和height = 100,如果你想要一个圆形则应该相同,

imageButton.layer.cornerRadius = 50 // this value should be half from width or height (width = height)
imageButton.layer.masksToBounds = YES

//如果你想要上面的边框

 imageButton.layer.borderWidth = 1;
 imageButton.layer.borderColor = [[UIColor blackColor] CGColor];

答案 1 :(得分:0)

你可以这样做。

borderView = UIView()

yourButton = UIButton()

borderView.frame.size.width = yourButton.frame.size.width + 1

borderView.layer.cornerRadius = borderView.frame.size.width/2

borderView.clipToBounds = true

borderView.backgroundColor = UIColor.clearColor()

borderView.layer.borderWidth = 1

borderView.layer.borderColor = UIColor.blueColor()