Search code examples
iosobjective-cuitoolbaruitoolbaritem

Adding borders to button in numberpad & add + sign on empty space


So I have been looking around and trying different codes and I cant really achieve what I want. I hope to find what I seek here.

I am trying to make a custom number pad. This is the result I want:

enter image description here

But this is as close I get.

enter image description here

First problem is that I cant get the apply and cancel button to have borders. How can i fix that?

The second problem is that I want to add the +*# button on my number pad also. how on earth can i do that?

This is the code im working with:

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;

Solution

  • First problem is that i cant get the apply and cancel button to have borders. How can i fix that?

    As you've been told, buttons do not have borders. So there is nothing to "fix". If you insist on borders, you will have to draw the background image of the button yourself, in such a way that it has something that looks like a border. Here is some code that I use in one my apps:

    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)