简体   繁体   English

自定义iOS6中的导航栏按钮

[英]Customize navigation bar button in iOS6

Update - One Possible Solution 更新 - 一种可能的解决方案

Based on answer given by Alexander Merchi in this post ( UIBarButtonItem Custom view in UINavigationBar ), a solution is found. 根据Alexander Merchi在这篇文章( UINavigationBar中的UIBarButtonItem Custom视图)中给出的答案,找到了一个解决方案。 But still don't know how to change the position of the image properly. 但仍然不知道如何正确改变图像的位置。

UIButton *btn =  [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setFrame:CGRectMake(0, 0, 28.0f, 28.0f)]; // 28 points is the width or height of the button image
        [btn setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(onClickMenuButton) forControlEvents:UIControlEventTouchUpInside];
        btnMenu = [[UIBarButtonItem alloc] initWithCustomView:btn];

在此输入图像描述

Original Post 原帖

In iOS 6, the goal is like this: 在iOS 6中,目标是这样的:

在此输入图像描述

while current result is like this: 而目前的结果是这样的:

在此输入图像描述

My code is 我的代码是

btnMenu = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"button.png"]
                                           style:UIBarButtonItemStylePlain
                                          target:self 
                                          action:@selector(onClickMenuButton)];

button.png is this the white circle with white three bars inside and a tranparent background. button.png是白色的圆圈,里面有白色的三个条纹和一个透明的背景。

UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
rightButton.frame = CGRectMake(0, 0, 30, 30);
[rightButton setImage:[UIImage imageNamed:@"Button-normal"] forState:UIControlStateNormal];
[rightButton setImage:[UIImage imageNamed:@"logout-hover"] forState:UIControlStateHighlighted];
[rightButton addTarget:self action:@selector(logOut) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
self.navigationItem.leftBarButtonItem = rightBarButtonItem;

I am accomplishing it by 我正在完成它

 UIImageView *uView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"btn_back_ontouch_scr11.png"]];
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.bounds = CGRectMake( 0, 0, uView.frame.size.width, uView.frame.size.height );
    [leftButton setImage:uView.image forState:UIControlStateHighlighted];
    [leftButton setImage:[UIImage imageNamed:@"btn_back_normal_scr11.png"]forState:UIControlStateNormal];
    [leftButton addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *aButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.scrollView.indicatorStyle=UIScrollViewIndicatorStyleWhite;

    self.navigationItem.leftBarButtonItem = aButton;
    self.navigationItem.hidesBackButton = YES;

This should work. 这应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM