我只是想知道是否可以在UINavigationControllers标题栏中添加UIButton或图标?
我问,因为我正在寻找一个一致的位置来显示一个“关于”按钮。
答案 0 :(得分:7)
您可以通过视图控制器的 viewDidLoad 方法执行此操作:
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem* infoButton = [[UIBarButtonItem alloc] initWithTitle:@"Info"
style:UIBarButtonItemStylePlain
target:self
action:@selector(infoButtonSelected:)];
self.navigationItem.rightBarButtonItem = infoButton;
[infoButton release];
}
在关于按下按钮的示例中,您还应该使用以下方法:
- (void)infoButtonSelected:(id)sender {
NSLog(@"button tapped");
// whatever needs to happen when button is tapped
}
答案 1 :(得分:0)
您将它添加到当前控制器navigationItem。它有leftBarButtonItem和rightBarButtonItem属性。
答案 2 :(得分:0)
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)
{
}
答案 3 :(得分:0)
navigationBar只是一个UIView,所以你可以创建一个UIButton和addSubview。 它必须是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];
}