自定义UINavigationBar 和 UIBarButtonItem(转)

步骤:
 
(1)创建Navigation的应用
(2)修改RootViewController为如下代码(笔者将两个文件合并一起,只为方便阅读)
(3)添加相应的图片到Resources
(4)Build and Run
 
#import <UIKit/UIKit.h>
@interface RootViewController : UITableViewController {}
@end
@implementation UINavigationBar (CustomImage)
- ( void)drawRect:( CGRect)rect {
     UIImage *image = [ UIImage  imageNamed@"nav.png"];
    [image  drawInRect : CGRectMake ( 0 ,  0 ,  self .frame .size .width ,  self .frame .size .height )];
}
@end
 
@implementation RootViewController
 
- ( id)initWithOwner:( UIViewController *)owner{
NSLog (@"gogo" );
UIButton *playNowButton = [[ UIButton  allocinitWithFrame: CGRectMake( 20006432)];
[playNowButton setBackgroundImage :[ UIImage imageNamed : @"left.png" ] forState :UIControlStateNormal ];
[playNowButton addTarget : self action : @selector (doSomething)forControlEvents :UIControlEventTouchDown ];
UIBarButtonItem  *bar =[[UIBarButtonItem  alloc ]  initWithCustomView :playNowButton];
[playNowButton  release];
//self.buttonOwner = owner;
return bar;
}
- ( id)initWithOwner1:( UIViewController *)owner{
NSLog (@"gogo" );
UIButton *playNowButton = [[ UIButton  allocinitWithFrame: CGRectMake( 10006432)];
[playNowButton setBackgroundImage :[ UIImage imageNamed : @"right.png" ] forState :UIControlStateNormal ];
[playNowButton addTarget : self action : @selector () forControlEvents :UIControlEventTouchDown ];
UIBarButtonItem  *bar =[[UIBarButtonItem  alloc ]  initWithCustomView :playNowButton];
[playNowButton  release];
//self.buttonOwner = owner;
return bar;
}
#pragma mark -
#pragma mark View lifecycle
 
- ( void)viewDidLoad {
UIBarButtonItem *buttonItem1 = [ self  initWithOwner1: self];
UIBarButtonItem *buttonItem = [ self  initWithOwner: self];
self .navigationItem .leftBarButtonItem  = buttonItem1;
self .navigationItem .rightBarButtonItem  = buttonItem;
    [ super viewDidLoad ];
}
 
- ( void)doSomething
{
NSLog (@"doSomething" );
[ self . navigationControlle  popToRootViewControllerAnimated : YES ];//pushViewController:self.recipeDetailView animated:YES];
}
#pragma mark -
#pragma mark Table view data source
 
// Customize the number of sections in the table view.
- ( NSInteger)numberOfSectionsInTableView:( UITableView *) tableView {
     return  1;
}
 
 
// Customize the number of rows in the table view.
- ( NSInteger)tableView:( UITableView *) tableView numberOfRowsInSection:( NSInteger)section {
     return  0;
}
 
 
// Customize the appearance of table view cells.
- ( UITableViewCell *)tableView:( UITableView *) tableView cellForRowAtIndexPath:( NSIndexPath*)indexPath {
 
     static  NSString *CellIdentifier =  @"Cell";
 
     UITableViewCell  *cell = [ tableView dequeueReusableCellWithIdentifier :CellIdentifier];
     if (cell ==  nil) {
        cell = [[[ UITableViewCell alloc ] initWithStyle :UITableViewCellStyleDefault reuseIdentifier :CellIdentifier] autorelease ];
    }
 
// Configure the cell.
 
     return cell;
}
 
- ( void)dealloc {
    [ super  dealloc];
}
 
 
@end

你可能感兴趣的:(c,职场,iPhone,休闲,Object-C)