【问题标题】:Custom UINavigationController with button带有按钮的自定义 UINavigationController
【发布时间】:2012-06-19 22:10:33
【问题描述】:

我希望 NavigationBar 的行为相同,但希望更改它的外观。我在网上找到了很多方法,但我不确定哪一种是 iOS 5.0 的最佳结果。导航栏将如下所示:

【问题讨论】:

    标签: iphone ios uinavigationcontroller


    【解决方案1】:

    由于您的目标是 iOS 5,我肯定会使用 Appearance proxy 选择 customizing UINavigationBar。然后,您可以轻松设置自己的图像,它们将应用于您应用程序中的所有导航栏,无需子类化。

    您还可以通过customizing UIBarButtonItem自定义导航栏中的按钮。返回按钮有backButtonBackgroundImageForState:barMetrics:,其他按钮有backgroundImageForState:barMetrics:

    【讨论】:

    • 只是好奇,人们会针对低于 5.0 的任何内容吗?我的印象是没有多少开发人员这样做。
    • @mkral 一些顾问工作仍然必须涵盖 4.x,因为客户需要它,但大多数项目的目标是 5.0。
    • 另一个快速的问题,我将如何隐藏后退按钮。从示例中,我看到他们正在将图像插入按钮中,我希望后退箭头在导航栏中齐平。我不在乎它是否使用我只是不希望它可见的按钮。
    • @mkral 您可以自定义栏按钮项目(包括后退按钮)。我已经用自定义 UIBarButtonItem 的文档链接更新了我的答案。
    【解决方案2】:

    我也一直在寻找这个东西,也没有找到一个简单的解决方案!感谢我的一位朋友和示例代码,我们使用自定义导航栏类​​制作了它,该类可以导入到任何视图控制器类中。

    .h 文件:

    #import <UIKit/UIKit.h>
    
    @interface NATitleBar : UIView {
        NSInteger tag;
    }
    @property ( nonatomic) IBOutlet UIImageView *imageView;
    @property ( nonatomic) IBOutlet UILabel *label;
    @property ( nonatomic) IBOutlet UIButton *back;
    @property ( nonatomic) IBOutlet UIButton *home;
    
    /**
     * Supports UIButton-style adding targets
     */
    
    @end
    

    .m 文件:

    #import "NATitleBar.h"
    
    @implementation NATitleBar
    @synthesize imageView;
    @synthesize label;
    @synthesize back;
    @synthesize home;
    
    - (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    
    if (self) {
        NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"NATitleBar" owner:self options:nil];
        [self addSubview:[views objectAtIndex:0]];
    
        // customize the view a bit
        //self.imageView.layer.borderWidth = 1.0;
        //self.imageView.layer.borderColor = [UIColor colorWithWhite:0.4 alpha:0.4].CGColor;
        //self.imageView.clipsToBounds = YES;
        //self.imageView.layer.cornerRadius = 5.0;
    }
    
    
    return self;
    }
    
    
    #pragma mark - Overriden Setters / Getters
    
    - (void)setTag:(NSInteger)aTag {
    self.back.tag = aTag;
    }
    
    - (NSInteger)tag {
    return self.back.tag;
    }
    
    @end
    

    然后对于 Nib 文件,我们有以下内容:

    您可以在 Nib 文件中添加或删除图像,以根据需要制作 GUI。

    现在您必须将该类导入您希望使用自定义导航控制器拥有的任何视图控制器,并定义两种方法(或一种,如果您不想要“主页”按钮。在.h

    - (void) back;
    

    .m:

    - (void)back {
        [self.navigationController popViewControllerAnimated:YES];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-22
      • 2011-03-31
      • 1970-01-01
      • 1970-01-01
      • 2010-11-29
      • 2014-02-09
      • 2020-09-26
      • 1970-01-01
      相关资源
      最近更新 更多