自定义 navigation 颜色 右边按钮 iphone

来源:互联网 发布:mac option r 编辑:程序博客网 时间:2024/06/01 08:37

自定义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];

}

原创粉丝点击