自定义导航栏按钮UIBarButtonItem 文字或图片
2014-03-21 08:16
381 查看
在4.0里定义导航条按钮通常是生成普通按钮,再用它生成导航条专用按钮。
[java] view
plaincopy
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:@"button_main.png"]
forState:UIControlStateNormal];
[button addTarget:self action:@selector(GotoSettings)
forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(x, y, x1, x2);
UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:menu];
self.navigationItem.rightBarButtonItem = menuButton;
[button release];
[menuButton release];
如果是在导航条一边创建多个button,在4.0里是通过segmentcontrol来间接实现
[java] view
plaincopy
UISegmentedControl *SegmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
@"开始",
@"暂停", nil]];
[SegmentedControl addTarget:self action:@selector(segmentAction:)
forControlEvents:UIControlEventValueChanged];
SegmentedControl.frame = CGRectMake(0, 0, 80, 30);
SegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
SegmentedControl.momentary = YES;
SegmentedControl.tintColor = [UIColor colorWithHue:0.6 saturation:0.33 brightness:0.69 alpha:0];
//defaultTintColor = [segmentedControl.tintColor retain]; // keep track of this for later
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc]
initWithCustomView:SegmentedControl];
self.navigationItem.rightBarButtonItem = segmentBarItem;
之后 通过Action方法判断是哪个button被按下
[java] view
plaincopy
- (void)segmentAction:(id)sender
{
//NSLog(@"segmentAction: selected segment = %d", [sender selectedSegmentIndex]);
if ([sender selectedSegmentIndex] == 0) {
//[self startAll];
}else if ([sender selectedSegmentIndex] == 1) {
//[self stopAll];
}
}
在iOS 5.0中,导航条引入了新的方法 setLeftBarButtonItems:animated:和setRightBarButtonItems:animated:来直接定义左右侧的多个button,方便了许多
[java] view
plaincopy
UIBarButtonItem *startBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(startDownloadAll)];
UIBarButtonItem *pauseBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPause target:self action:@selector(stopDownloadAll)];
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects: pauseBtn,startBtn,nil]];
转自:http://blog.csdn.net/bjash/article/details/9945405#1536434-tsina-1-71305-66a1f5d8f89e9ad52626f6f40fdeadaa
[java] view
plaincopy
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:@"button_main.png"]
forState:UIControlStateNormal];
[button addTarget:self action:@selector(GotoSettings)
forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(x, y, x1, x2);
UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:menu];
self.navigationItem.rightBarButtonItem = menuButton;
[button release];
[menuButton release];
如果是在导航条一边创建多个button,在4.0里是通过segmentcontrol来间接实现
[java] view
plaincopy
UISegmentedControl *SegmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
@"开始",
@"暂停", nil]];
[SegmentedControl addTarget:self action:@selector(segmentAction:)
forControlEvents:UIControlEventValueChanged];
SegmentedControl.frame = CGRectMake(0, 0, 80, 30);
SegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
SegmentedControl.momentary = YES;
SegmentedControl.tintColor = [UIColor colorWithHue:0.6 saturation:0.33 brightness:0.69 alpha:0];
//defaultTintColor = [segmentedControl.tintColor retain]; // keep track of this for later
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc]
initWithCustomView:SegmentedControl];
self.navigationItem.rightBarButtonItem = segmentBarItem;
之后 通过Action方法判断是哪个button被按下
[java] view
plaincopy
- (void)segmentAction:(id)sender
{
//NSLog(@"segmentAction: selected segment = %d", [sender selectedSegmentIndex]);
if ([sender selectedSegmentIndex] == 0) {
//[self startAll];
}else if ([sender selectedSegmentIndex] == 1) {
//[self stopAll];
}
}
在iOS 5.0中,导航条引入了新的方法 setLeftBarButtonItems:animated:和setRightBarButtonItems:animated:来直接定义左右侧的多个button,方便了许多
[java] view
plaincopy
UIBarButtonItem *startBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(startDownloadAll)];
UIBarButtonItem *pauseBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPause target:self action:@selector(stopDownloadAll)];
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects: pauseBtn,startBtn,nil]];
转自:http://blog.csdn.net/bjash/article/details/9945405#1536434-tsina-1-71305-66a1f5d8f89e9ad52626f6f40fdeadaa
相关文章推荐
- 自定义导航栏按钮UIBarButtonItem 文字或图片
- 自定义导航栏按钮UIBarButtonItem 文字或图片
- 自定义导航栏按钮UIBarButtonItem 文字或图片
- 自定义导航栏按钮UIBarButtonItem 文字或图片(定制)
- 自定义导航栏按钮UIBarButtonItem 文字或图片
- 详解iOS应用中自定义UIBarButtonItem导航按钮的创建方法
- iOS开发>学无止境 - 自定义导航按钮UIBarButtonItem
- UIBarButtonItem类目-返回一个有图片的导航栏按钮
- 自定义导航按钮UIBarButtonItem
- 自定义导航按钮UIBarButtonItem
- 自定义导航按钮UIBarButtonItem
- 自定义导航按钮UIBarButtonItem
- iOS之自定义导航按钮UIBarButtonItem的样式
- 自定义导航按钮UIBarButtonItem
- 自定义导航按钮UIBarButtonItem
- 自定义导航按钮UIBarButtonItem
- 如果直接通过获得所有的UIBarButtonItem更改起返回按钮的图片-出现的问题
- UIBarButtonItem-添加自定义Left或者Right按钮
- 02---按钮的设置 控制器拥有导航栏包装一层导航控制器 添加子控制器 UIBarButtonItem导航按钮 设置导航栏UINavigationBar主题 设置状态栏样式
- UIBarButtonItem-添加自定义Left或者Right按钮 <总结>