iOS-如何自定义导航后退栏项目的垂直位置

时间:2018-11-29 08:44:51

标签: ios objective-c

我有一个这样的导航栏设计。我已经准备好增加导航栏的高度,但是如何设置“后退”按钮图像的位置并使标题左对齐?

#import "UINavigationBar+Custom.h"

@implementation UINavigationBar (Custom)

- (CGSize)sizeThatFits:(CGSize)size {
    CGRect rec = self.frame;
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    rec.size.width = screenRect.size.width;
    rec.size.height = 77;
    return rec.size;
}

@end

NavBar图片设计

enter image description here

现实

enter image description here

更新:我的代码以添加后退按钮

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configNavBar];
}

- (void)configNavBar {
    // -- Config NavigationBar Background --
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;

    // -- Navigator title font --
    NSDictionary *titleAttributes = @{
                                      NSFontAttributeName :FONT_NAVIGATION_TITLE_BAR,
                                      NSForegroundColorAttributeName : [Utils colorWithHexString:@"#0f385a"] };
    [self.navigationController.navigationBar setTitleTextAttributes:titleAttributes];

    // -- Back button color --
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    UIImage *myImage = [UIImage imageNamed:@"ic_arrow_back"]; //set your backbutton imagename
    UIImage *backButtonImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    // now use the new backButtomImage
    [[UINavigationBar appearance] setBackIndicatorImage:backButtonImage];
    [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:backButtonImage];

    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(-0.f, -0)
                                                         forBarMetrics:UIBarMetricsDefault];
    [[UIBarButtonItem appearance] setBackgroundVerticalPositionAdjustment:40 forBarMetrics:UIBarMetricsDefault];

    // -- Empty back title --
    UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    [self.navigationItem setBackBarButtonItem:backButtonItem];
}

1 个答案:

答案 0 :(得分:0)

尝试使用此代码UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"image"] style:UIBarButtonItemStylePlain target:self action:@selector(action)]; [self.navigationItem setLeftBarButtonItem:barButtonItem];。并正确格式化图像