【问题标题】:Adding borders to button in numberpad & add + sign on empty space为数字键盘中的按钮添加边框并在空白处添加 + 符号
【发布时间】:2015-01-20 19:00:58
【问题描述】:

所以我一直在环顾四周并尝试不同的代码,但我无法真正实现我想要的。我希望在这里找到我想要的东西。

我正在尝试制作自定义数字键盘。这是我想要的结果:

但这是我得到的最接近的。

第一个问题是我无法让应用和取消按钮有边框。我该如何解决?

第二个问题是我还想在我的数字键盘上添加 +*# 按钮。我到底该怎么做?

这是我正在使用的代码:

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;

【问题讨论】:

  • 拥有一个标题/图像为"+*#"的按钮怎么样?
  • 第一张图片是旧的 iOS 6 和之前的外观。下图是新的 iOS 7 及更高版本的外观。按钮不再有边框了。
  • 使用电话类型键盘而不是数字键盘。
  • Lol thnx maddy。哈哈我从来没有见过这个选项。 @maddy

标签: ios objective-c uitoolbar uitoolbaritem


【解决方案1】:

第一个问题是我无法让应用和取消按钮有边框。我该如何解决?

如您所说,按钮没有边框。所以没有什么可以“修复”的。如果您坚持使用边框,则必须自己绘制按钮的背景图像,使其具有看起来像边框的东西。这是我在一个应用中使用的一些代码:

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)

【讨论】:

  • 如果你有两个不相关的问题(你有),请提出两个单独的问题。
【解决方案2】:

在 iOS 7 中,默认的 UIButton 是透明背景,没有边框。在 iOS7+ 中,如果你想像 iOS 6 一样拥有角落和背景,请使用自定义按钮。

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;

【讨论】:

    猜你喜欢
    • 2021-09-04
    • 1970-01-01
    • 1970-01-01
    • 2012-11-10
    • 2023-03-15
    • 2011-05-20
    • 2012-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多