【问题标题】:Customize navigation bar button in iOS6在 iOS6 中自定义导航栏按钮
【发布时间】:2013-12-02 08:54:35
【问题描述】:

更新 - 一种可能的解决方案

根据 Alexander Merchi 在这篇文章 (UIBarButtonItem Custom view in UINavigationBar) 中给出的答案,找到了解决方案。但是还是不知道如何正确改变图片的位置。

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];

原帖

在 iOS 6 中,目标是这样的:

虽然目前的结果是这样的:

我的代码是

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

button.png 这个是白色的圆圈,里面有三个白色的条,背景是透明的。

【问题讨论】:

  • 在我的回答中更改 leftButton.bounds。它应该可以改变位置。

标签: ios uinavigationbar customization


【解决方案1】:
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;

【讨论】:

  • 致@Dinesh,你知道如何改变图像的位置吗?我试图给一些偏移量,比如 CGRectMake(10, 0, 30, 30);但图像没有移动。
  • 由于您将自定义按钮设置为条形按钮,我认为这对您没有多大帮助..因为它总是会尝试采用条形按钮本身的 x 轴..
  • 谢谢你的帮助,我明天试试其他方法:D
  • 这个按钮是白色的,我怎样才能把它变成黑色?
【解决方案2】:

我正在完成它

 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;

这应该可行。

【讨论】:

  • 嗨@mihirmehta,你知道如何改变图像的位置吗?我试图给一些偏移量,比如 CGRectMake(10, 0, 30, 30);但图像没有移动。
  • 它应该移动。最好让你的设计师改变图像,使其看起来适合当前帧
猜你喜欢
  • 2014-01-31
  • 2011-09-04
  • 2014-05-11
  • 1970-01-01
  • 1970-01-01
  • 2016-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多