Skip to content

Instantly share code, notes, and snippets.

@jsai
Last active Dec 26, 2015
Embed
What would you like to do?
Bar button item padding
- (void)setupBackButtonWithSelector:(SEL)selector {
// create a custom button
LIButton *backButton = [LIButton buttonWithType:UIButtonTypeCustom];
backButton.positionInNavBar = LIButtonNavPositionLeft;
// more back button setup
// add back button to navigation bar
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backItem;
}
/*! Position in navigation bar
*/
typedef enum LIButtonNavPosition {
LIButtonNavPositionNone = 0,
LIButtonNavPositionLeft,
LIButtonNavPositionRight
} LIButtonNavPosition;
@interface LIButton : UIButton
/*! Position of button when used as Custom View in navigation bar.
*/
@property (nonatomic) LIButtonNavPosition positionInNavBar;
@end
@implementation LIButton
/*! Override and return insets based on location in navigation bar.
*/
- (UIEdgeInsets)alignmentRectInsets {
if (LI_IS_IOS7_RUNTIME) {
switch (self.positionInNavBar) {
case LIButtonNavPositionLeft:
return UIEdgeInsetsMake(0.0, 12.0, 0.0, 0.0);
break;
case LIButtonNavPositionRight:
return UIEdgeInsetsMake(0.0, 0.0, 0.0, 12.0);
break;
default:
return UIEdgeInsetsZero;
break;
}
}
return UIEdgeInsetsZero;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment