0

I need to add a button with no title and background image and perform action when clicked on it .I need to add a button to navigation bar. I have made the navigation bar like this :

-(void)viewDidLoad{
[super viewDidLoad];
    [self.navigationBar setFrame:CGRectMake(0,0,320,50)];
    self.navigationBar.tintColor=[UIColor whiteColor];
}

I can see the white navigation bar to it.How can I add a button to it with image and action ?

4

3 回答 3

1

使用此代码

-(void)viewDidLoad{
[super viewDidLoad];
    [self.navigationBar setFrame:CGRectMake(0,0,320,50)];
    self.navigationBar.tintColor=[UIColor whiteColor];

   UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"yourImageName"]
                         style:UIBarButtonItemStyleBordered
                        target:self action:@selector(yourButtonPressMethodName:)];   
    self.navigationItem.leftBarButtonItem = item;//use rightBarButtonItem if you want to add button on right dife
}
于 2013-01-04T11:13:38.877 回答
0

您可以使用以下代码:

UIBarButtonItem *rightBar=[[UIBarButtonItem alloc]initWithImage:@"img.png" style:UIBarButtonItemStyleBordered target:self action:@selector(methodname:)];
于 2013-01-04T11:13:29.640 回答
0

您可以UIBarButtonItem使用自定义视图进行制作:-

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0.0, 0.0, 75.0, 40.0)];
[btn setImage:[UIImage imageNamed:@"anyImage.png"] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(yourAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barbutton = [[UIBarButtonItem alloc]initWithCustomView:btn];
self.navigationItem.leftBarButtonItem = barbutton;
于 2013-01-04T11:23:40.667 回答