views:

193

answers:

3

Hi,

I'm just wondering if its at all possible to add a UIButton or and icon to a UINavigationControllers title bar?

I'm asking because I'm seeking a consistent position to display an "about" button.

A: 

You add it to the current controllers navigationItem. It's got the leftBarButtonItem and rightBarButtonItem properties.

Joonas Trussmann
+2  A: 

You can do that via the viewDidLoad method of the view controller:

- (void)viewDidLoad {
    [super viewDidLoad];

    UIBarButtonItem* infoButton = [[UIBarButtonItem alloc] initWithTitle:@"Info" 
                                                                   style:UIBarButtonItemStylePlain 
                                                                  target:self 
                                                                  action:@selector(infoButtonSelected:)];
    self.navigationItem.rightBarButtonItem = infoButton;
    [infoButton release];
}

In the example about when the button is tapped, you should also have the method:

- (void)infoButtonSelected:(id)sender {
        NSLog(@"button tapped");
        // whatever needs to happen when button is tapped
}
Ronnie Liew
The example you gave is perfect. Thanks a lot!
dubbeat
A: 

UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems: [NSArray arrayWithObjects: [UIImage imageNamed:@"change frame btn.png"], [UIImage imageNamed:@"im.jpeg"], // [UIImage imageNamed:@"im.jpeg"], nil]]; [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged]; segmentedControl.frame = CGRectMake( 0, 0, 150, 30); segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar; segmentedControl.momentary = YES;

UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];

self.navigationItem.rightBarButtonItem = segmentBarItem;
[segmentBarItem release];

}

- (void)segmentAction:(id)sender{

    if([sender selectedSegmentIndex] == 0)
    {

} if([sender selectedSegmentIndex] == 1) {

Ajeet Kumar Yadav
hi, could you reformat your code so that I can read it properly ?? :)
dubbeat