ios - 如何向 UINavigationBar 添加按钮

标签 ios uinavigationcontroller uibarbuttonitem

我需要这样的东西 http://anishusite.appspot.com/hbimgs/Bills220.png

能够从导航栏更改日期...这是如何完成的? 我尝试添加一个带有数千个按钮的工具栏和一个带有日期标签的 View ,但效果不佳

http://osmorphis.blogspot.ro/2009/05/multiple-buttons-on-navigation-bar.html

这是代码:

  //first create the view 
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 6, 157, 33)];
[titleView setBackgroundColor:[UIColor clearColor]];

//add the buttons to the view
UIButton *prevButton = [UIButton buttonWithType:UIButtonTypeCustom];
[prevButton setFrame:CGRectMake(0, 1, 32, 32)];
[prevButton setBackgroundImage:[UIImage imageNamed:@"36-circle-west"] forState:UIControlStateNormal];
[prevButton addTarget:self action:@selector(prevMonth) forControlEvents:UIControlEventTouchUpInside];
[titleView addSubview:prevButton];


UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
[nextButton setFrame:CGRectMake(145, 1, 32, 32)];
[nextButton setBackgroundImage:[UIImage imageNamed:@"20-circle-east"] forState:UIControlStateNormal];
[nextButton addTarget:self action:@selector(nextMonth) forControlEvents:UIControlEventTouchUpInside];
[titleView addSubview:nextButton];

[self.dateLabel setFrame:CGRectMake(33, 1, 90, 33)];
[self.dateLabel setBackgroundColor:[UIColor clearColor]];
[self.dateLabel setTextAlignment:UITextAlignmentCenter];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterNoStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateFormat:@"MMMM,YYYY"];
NSString *dateString = [dateFormatter stringFromDate:self.beginDate];
NSLog(@"the title date string: %@",dateString);

[self.dateLabel setText:dateString];
[self.dateLabel setTextColor:[UIColor whiteColor]];
[self.dateLabel setBackgroundColor:[UIColor clearColor]];
[self.dateLabel setFont:[UIFont fontWithName:@"System" size:17.0]];

//create the toolbar itself

UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 240, 44)];
[toolbar setBackgroundColor:[UIColor clearColor]];

NSMutableArray *items  = [NSMutableArray arrayWithCapacity:3];

UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addTransaction)];
addButton.style = UIBarButtonItemStyleBordered;


UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

UIBarButtonItem *theView = [[UIBarButtonItem alloc] initWithCustomView:titleView];

[items addObject:theView];
[items addObject:spacer];
[items addObject:addButton];

[toolbar setItems:items animated:NO];

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbar];

http://www.shareimages.com/image.php?61601-qpyWmpSfk5.kmpyWmak-screen.png

正如你所看到的,它看起来不太好。标签未显示...这些按钮似乎只有在点击上半部分时才起作用。 (点击时,方法被调用 ok )...有什么想法吗?顺便说一句,导航栏有一个背景图像,但是工具栏掩盖了它。为什么?我用清晰的彩色背景制作了工具栏....

最佳答案

UINavigationItem 有一个 titleView 属性。您可以将其设置为您创建的任何 View 。因此,创建一个具有透明背景的 UIToolbar 子类,向其中添加按钮和文本标签,然后:

self.navigationItem.titleView = myCustomView;

其中 myCustomView 是包含箭头按钮和标签的 View 。该 View 可以是 UIToolbar 子类,请阅读 Couldn't UIToolBar be transparent?有关创建透明 UIToolbar 子类的技巧,请添加按钮:

弹性间隔、左箭头、带有堆叠 UILabels 的自定义 View 、右箭头、弹性间隔

关于ios - 如何向 UINavigationBar 添加按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12075953/

相关文章:

iphone - iPad UINavigationController 自定义栏背景图像

objective-c - 在导航 Controller 上方添加放大镜

uibarbuttonitem - BarButtonItems 和标题未出现在 iOS 11 中

iphone - 将自定义选择器添加到 UIBarButtonItem

ios - UICollectionViewCell内部的UITextView坚持水平增长

iOS Objective C 方法问题

objective-c - 为什么 iOS 类不采用 copyWithZone 协议(protocol)来鼓励 active mem mgt?

ios - 以编程方式调用表情符号键盘?

ios - UINavigationController 如何在 subview 中设置标题等

ios - 如何在ipad中为UIBarButton项目设置图像