Zar E Ahmer
Zar E Ahmer

Reputation: 34380

Change background of a uinavigation right button?

I am successful in changing the text of the right navigation button by given code. But i also want to change it has a delfault background .

I simply want to show a text with transparent background.

I have also tried

 UIBarButtonItem *rightBtn=[[UIBarButtonItem alloc] initWithTitle:@"MyTitle" style:UIBarButtonItemStyleDone target:self action:@selector(myMethod)];

rightBtn.tintColor =[UIColor clearColor];

but still showing a button background.

Is there a way to achieve it by changing any different style which don't have background. or is there an other simple way to change button to a text with no transparent background.

Upvotes: 1

Views: 51

Answers (2)

Rabby Alam
Rabby Alam

Reputation: 300

You can use this:

 UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
    btn.backgroundColor=[UIColor clearColor];
    [btn setTitle:@"Title" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(myMethod) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBtn=[[UIBarButtonItem alloc] initWithCustomView:btn];


    [self.navigationItem setRightBarButtonItem:rightBtn];

Upvotes: 1

Nick
Nick

Reputation: 949

Add this in viewDidLoad:

[self.navigationItem.rightBarButtonItem setBackgroundImage:[UIImage new] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

Upvotes: 0

Related Questions