自定义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 *p_w_picpath = [UIImage p_w_picpathNamed: @"nav.png"];
[p_w_picpath drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end
@implementation RootViewController
- (id)initWithOwner:(UIViewController *)owner{
NSLog(@"gogo");
UIButton *playNowButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 0, 64, 32)];
[playNowButton setBackgroundImage:[UIImage p_w_picpathNamed:@"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 alloc] initWithFrame:CGRectMake(100, 0, 64, 32)];
[playNowButton setBackgroundImage:[UIImage p_w_picpathNamed:@"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
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
SpringBoot自定义starter
SpringBoot自定义starter
spring maven ci -
Android 自定义标签 和 自定义组件
1 自定义标签这是我的
android xml 自定义组件