为数字键盘中的按钮添加边框并在空白处添加 + 号 space
Adding borders to button in numberpad & add + sign on empty space
所以我一直在四处寻找并尝试不同的代码,但我无法真正实现我想要的。我希望能在这里找到我想要的。
我正在尝试制作自定义数字键盘。这是我想要的结果:
但这已经很接近了。
第一个问题是我无法让应用和取消按钮有边框。我该如何解决?
第二个问题是我也想在数字键盘上添加 +*# 按钮。我到底该怎么做?
这是我正在使用的代码:
self.numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
self.numberToolbar.barStyle = UIBarStyleBlackTranslucent;
self.numberToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(cancelNumberPad)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:@"Apply" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)],
nil];
[self.numberToolbar sizeToFit];
self.driverNumber.inputAccessoryView = self.numberToolbar;
First problem is that i cant get the apply and cancel button to have borders. How can i fix that?
正如您所知,按钮没有边框。所以没有什么可以"fix"。如果您坚持使用边框,则必须自己绘制按钮的背景图像,使其看起来像边框。这是我在我的一个应用程序中使用的一些代码:
b.setBackgroundImage(imageOfSize(CGSizeMake(15,15)) {
let grad = CAGradientLayer()
grad.frame = CGRectMake(0,0,15,15)
grad.colors = [
UIColor(red: 1, green: 1, blue: 0, alpha: 0.8).CGColor,
UIColor(red: 0.7, green: 0.7, blue: 0.3, alpha: 0.8).CGColor]
let p = UIBezierPath(
roundedRect: CGRectMake(0,0,15,15), cornerRadius: 8)
p.addClip()
grad.renderInContext(UIGraphicsGetCurrentContext())
UIColor.blackColor().setStroke()
p.lineWidth = 2
p.stroke()
}.resizableImageWithCapInsets(
UIEdgeInsetsMake(7,7,7,7), resizingMode: .Stretch),
forState: .Normal)
在iOS7中默认的UIButton是透明背景无边框的。在 iOS7+ 中,如果你想要像 iOS6 这样的角和背景,请使用自定义按钮。
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
现在您可以根据需要设置背景、边框和圆角半径。
button.layer.cornerRadius = 2.0f;
button.layer.borderWidth = 1.0f;
button.layer.borderColor = [UIColor whiteColor].CGColor;
button.backgroundColor = [UIColor blueColor];
button.clipsToBounds = YES;
所以我一直在四处寻找并尝试不同的代码,但我无法真正实现我想要的。我希望能在这里找到我想要的。
我正在尝试制作自定义数字键盘。这是我想要的结果:
但这已经很接近了。
第一个问题是我无法让应用和取消按钮有边框。我该如何解决?
第二个问题是我也想在数字键盘上添加 +*# 按钮。我到底该怎么做?
这是我正在使用的代码:
self.numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
self.numberToolbar.barStyle = UIBarStyleBlackTranslucent;
self.numberToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(cancelNumberPad)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:@"Apply" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)],
nil];
[self.numberToolbar sizeToFit];
self.driverNumber.inputAccessoryView = self.numberToolbar;
First problem is that i cant get the apply and cancel button to have borders. How can i fix that?
正如您所知,按钮没有边框。所以没有什么可以"fix"。如果您坚持使用边框,则必须自己绘制按钮的背景图像,使其看起来像边框。这是我在我的一个应用程序中使用的一些代码:
b.setBackgroundImage(imageOfSize(CGSizeMake(15,15)) {
let grad = CAGradientLayer()
grad.frame = CGRectMake(0,0,15,15)
grad.colors = [
UIColor(red: 1, green: 1, blue: 0, alpha: 0.8).CGColor,
UIColor(red: 0.7, green: 0.7, blue: 0.3, alpha: 0.8).CGColor]
let p = UIBezierPath(
roundedRect: CGRectMake(0,0,15,15), cornerRadius: 8)
p.addClip()
grad.renderInContext(UIGraphicsGetCurrentContext())
UIColor.blackColor().setStroke()
p.lineWidth = 2
p.stroke()
}.resizableImageWithCapInsets(
UIEdgeInsetsMake(7,7,7,7), resizingMode: .Stretch),
forState: .Normal)
在iOS7中默认的UIButton是透明背景无边框的。在 iOS7+ 中,如果你想要像 iOS6 这样的角和背景,请使用自定义按钮。
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
现在您可以根据需要设置背景、边框和圆角半径。
button.layer.cornerRadius = 2.0f;
button.layer.borderWidth = 1.0f;
button.layer.borderColor = [UIColor whiteColor].CGColor;
button.backgroundColor = [UIColor blueColor];
button.clipsToBounds = YES;