Next/Previous Keyboard Toolbar iOS7

前端 未结 7 1088
情书的邮戳
情书的邮戳 2021-02-01 05:33

Currently, I am trying to set the next/previous buttons on my keyboard toolbar to the new, sleek iOS 7 back button/forward buttons that get put in navigation controllers to go b

相关标签:
7条回答
  • 2021-02-01 05:36

    1) Download latest files from: https://github.com/simonbs/BSKeyboardControls

    2) Import the images for back/next buttons. These can be whatever you want and you can set the appropriate sizes so that they look good. Joshua has a good set. I have mine saved as "keyboardBack.png" and "keyboardForward.png"

    3) In BSKeyboardControls.m, update initWithFields:fields. Here you can do some customization like setting the width of your back/next buttons. I removed the Done button here too to follow your screenshot but you can add it back.

    - (id)initWithFields:(NSArray *)fields
    {
        if (self = [super initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)])
        {
            // Creates toolbar
            [self setToolbar:[[UIToolbar alloc] initWithFrame:self.frame]];
            [self.toolbar setBarStyle:UIBarStyleDefault];
            [self.toolbar setAutoresizingMask:(UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth)];
            [self addSubview:self.toolbar];
    
            // Import images
            UIImage *backImage = [[UIImage imageNamed:@"keyboardBack"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            UIImage *forwardImage = [[UIImage imageNamed:@"keyboardForward"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
            // Create segmentedcontrol
            self.segmentedControl = [[UISegmentedControl alloc] initWithItems:@[backImage, forwardImage]];
            self.segmentedControl.tintColor = [UIColor clearColor];
    
            // Set button widths
            [self.segmentedControl setWidth:50 forSegmentAtIndex:0];
            [self.segmentedControl setWidth:50 forSegmentAtIndex:1];
    
            // Other BSKeyboardControls stuff
            [self.segmentedControl addTarget:self action:@selector(segmentedControlValueChanged:) forControlEvents:UIControlEventValueChanged];
            [self.segmentedControl setMomentary:YES];
            [self.segmentedControl setEnabled:NO forSegmentAtIndex:BSKeyboardControlsDirectionPrevious];
            [self.segmentedControl setEnabled:NO forSegmentAtIndex:BSKeyboardControlsDirectionNext];
            [self setSegmentedControlItem:[[UIBarButtonItem alloc] initWithCustomView:self.segmentedControl]];
            [self setVisibleControls:(BSKeyboardControlPreviousNext)];
            [self setFields:fields];
        }
    
        return self;
    }
    

    4) The left padding on the toolbar is a bit too much, so you can fix it by adding a negative separator in toolbarItems: in BSKeyboardControls.m:

    - (NSArray *)toolbarItems
    {
        NSMutableArray *items = [NSMutableArray arrayWithCapacity:3];
        if (self.visibleControls & BSKeyboardControlPreviousNext)
        {
            UIBarButtonItem *negativeSeperator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                                                                               target:nil
                                                                                               action:nil];
            negativeSeperator.width = -12;
    
            [items addObject:negativeSeperator];
            [items addObject:self.segmentedControlItem];
    
        }
    
        if (self.visibleControls & BSKeyboardControlDone)
        {
            [items addObject:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]];
            [items addObject:self.doneButton];
        }
    
        return items;
    }
    

    Note: I probably don't have the button widths and paddings to the exact specs, but you can tweak it to your liking!

    0 讨论(0)
  • 2021-02-01 05:38

    Using Xcode 7.3, you can also just enter a symbol as the title for the Bar Item. This works both in code and in Interface Builder:

    • Place your cursor where you want the symbol to appear (eg, title box in IB or within quotes in code).
    • In Xcode's menu, click Edit, then Emoji & Symbols. (Alternatively, press control-command-space to bring up the list of characters.)
    • In the search box, type less or greater.
    • Then, select the symbol you want.

    The less-than and greater-than symbols are colored blue in the toolbar by default.

    In code:

    backButton.title = "<"
    

    In IB:

    0 讨论(0)
  • 2021-02-01 05:40

    These are the images used in toolbar, e.g. the back and forward button images:

    enter image description here

    enter image description here

    0 讨论(0)
  • 2021-02-01 05:45

    You can use this great tool by @iftekhar and customise IQSegmentedNextPrevious according to your need for giving images instead of next previous button.

    0 讨论(0)
  • 2021-02-01 05:46

    Use the icons shared by Joshua and then try with below piece of code and you will be good to go. I have not added the done button here.

    UIImage *backImage = [[UIImage imageNamed:@"backImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage *forwardImage = [[UIImage imageNamed:@"forward"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.segmentedControl = [[UISegmentedControl alloc] initWithItems:@[backImage, forwardImage]];
    [self.segmentedControl addTarget:self action:@selector(segmentedControlChangedState:) forControlEvents:UIControlEventValueChanged];
    self.segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    self.segmentedControl.tintColor = [UIColor clearColor];
    UIBarButtonItem *aSegmentedControlBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.segmentedControl];
    [self setItems:@[aSegmentedControlBarButtonItem, self.flexibleSpace]];
    
    0 讨论(0)
  • 2021-02-01 05:52

    If you don't mind living on the edge you could use Apple's undocumented system items to achieve the iOS 7 look. Here are left and right bar button items.

        [self setDoneButton:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:105 target:nil action:nil]];
        [self setDoneButton:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:106 target:nil action:nil]];
    

    Source: http://iphonedevwiki.net/index.php/UIBarButtonItem

    0 讨论(0)
提交回复
热议问题