Add uibutton to navigationController title bar iphone
4 answers
This can be done using 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 clicked, you should also have a method:
- (void)infoButtonSelected:(id)sender {
NSLog(@"button tapped");
// whatever needs to happen when button is tapped
}
+7
a source to share
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)
{
}
0
a source to share
navigationBar is just a UIView, so you can just create a UIButton and addSubview. it must be viewDidAppear NOT viewdidLoad.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// this must be AFTER The animation - otherwise it wont appear in the right spot
int xpos=self.navigationController.navigationBar.frame.size.width/2+60;
_toggleOnDeviceButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_toggleOnDeviceButton addTarget:self action:@selector(toggleButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[_toggleOnDeviceButton setFrame:CGRectMake(xpos,2,40,40)];
[_toggleOnDeviceButton setImage:[UIImage imageNamed:@"blank_button.png"] forState:UIControlStateNormal];
[_toggleOnDeviceButton setTitle:@"Go" forState:UIControlStateNormal];
_toggleOnDeviceButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
[self.navigationController.navigationBar addSubview:_toggleOnDeviceButton];
}
0
a source to share