自定义 navigation 颜色 右边按钮 iphone

本文介绍如何在iOS应用中自定义UINavigationBar的背景图片及UIBarButtonItem的左右侧按钮,并实现点击按钮后的警告提示功能。

自定义Navigation和NavigationItem

from http://www.codeios.com/thread-97-1-1.html

 

#import "NavTest.h"

 

@implementation UINavigationBar (CustomImage)

 

//定义navigation背景图片

- (void)drawRect:(CGRect)rect {

    UIImage *image = [UIImage imageNamed: @"bg.png"];

    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];

}

@end

 

@implementation NavTest

 

- (void)leftAction:(id)sender

{

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告" message:@"你点击左边的按钮"

                                 otherButtonTitles:nil];

        [alert show];

        [alert release];

}

 

- (void)rightAction:(id)sender

{

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告" message:@"你点击右边的按钮"

                                otherButtonTitles:nil];

        [alert show];

        [alert release];

}

 

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.

- (void)viewDidLoad {

 

        self.navigationItem.title = @"自定义";

        //定义左侧按钮

        UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 40)];

        [leftButton setImage:[UIImage imageNamed:@"titleBgButton.png"] forState:UIControlStateNormal];

        [leftButton setImage:[UIImage imageNamed:@"titleBgButton1.png"] forState:UIControlStateHighlighted];

        [leftButton addTarget:self action:@selector(leftAction:) forControlEvents:UIControlEventTouchUpInside];

        UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];

        [leftButton release];

 

        //定义右侧按钮

        UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 40)];

        [rightButton setImage:[UIImage imageNamed:@"titleBgButton.png"] forState:UIControlStateNormal];

        [rightButton setImage:[UIImage imageNamed:@"titleBgButton1.png"] forState:UIControlStateHighlighted];

        [rightButton addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside];

        UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];

        [rightButton release];

 

        self.navigationItem.leftBarButtonItem = leftItem;

        self.navigationItem.rightBarButtonItem = rightItem;

 

        [leftItem release];

        [rightItem release]; 

 

    [super viewDidLoad];

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值