添加与 inputAccessaryView 相同的工具栏时不显示 UIToolBar

Posted

技术标签:

【中文标题】添加与 inputAccessaryView 相同的工具栏时不显示 UIToolBar【英文标题】:UIToolBar is not shown when adding the same toolbar as inputAccessaryView 【发布时间】:2012-01-30 10:08:18 【问题描述】:

我有一个 uipickerview,它会在按下 uibutton 时被填充。 这个 uipickerview 在 uipickerview 的顶部有一个 uitoolbar 控件。 那个工具栏里面有 uibuttons。它工作正常,直到我做下面的事情。

我想在 uikeyboard 上拥有那个 uittoolbar。所以我添加了那个 uitoolbar 控件作为 InputAccessoryView。 当我这样做时,uitoolbar 带有 uikeyboard.that 很好。

但是当我点击按钮时,它必须显示带有该工具栏的 uipickerview,并且还需要退出工具栏。

所以当点击按钮时,我已经退出了 uikeyboard。 uipickerview 正在显示,但工具栏不可见。

我一直在努力解决这个问题,但确实面临着艰难的时期。

请告诉我。

- (void)viewDidLoad

[super viewDidLoad];

// Create an UIButton
self.btnClick = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[self.btnClick addTarget: self action:@selector(showPicker) 
    forControlEvents:UIControlEventTouchUpInside]; 
// Set frame width, height
self.btnClick.frame = CGRectMake(10, 100, 30, 30);    
self.btnClick.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[self.view addSubview:self.btnClick];

self.toolBar = [[[UIToolbar alloc] init]autorelease];
[self.toolBar sizeToFit];
self.toolBar.frame = CGRectMake(0,198, 320, 35);
self.toolBar.barStyle = UIBarStyleBlackTranslucent;

UIImage *image = [UIImage imageNamed:@"orangebuttonsmall.png"];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.bounds = CGRectMake( 0, 0, image.size.width, 25 );    
[aButton setImage:image forState:UIControlStateNormal];
[aButton addTarget:self action:@selector(pickerNumPadClicked:) forControlEvents:UIControlEventTouchUpInside];    
UIBarButtonItem *numButton = [[[UIBarButtonItem alloc] initWithCustomView:aButton]autorelease];

UIImage *aCenterimage = [UIImage imageNamed:@"button_normal.png"];
UIButton *myCenterBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[myCenterBtn setTitle:@"Poet Names" forState:UIControlStateNormal];
[myCenterBtn setBackgroundImage:aCenterimage forState:UIControlStateNormal];
myCenterBtn.frame = CGRectMake(100, 0,200,25);
myCenterBtn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
myCenterBtn.titleLabel.font = [UIFont systemFontOfSize:10];
myCenterBtn.titleLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5]; 
myCenterBtn.titleLabel.shadowOffset = CGSizeMake(0, -1);
myCenterBtn.bounds = CGRectInset(myCenterBtn.bounds, -3, 2);
myCenterBtn.titleLabel.font = [UIFont boldSystemFontOfSize:13];
[myCenterBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[myCenterBtn addTarget:self action:@selector(aCenterBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *flex = [[[UIBarButtonItem alloc] initWithCustomView:myCenterBtn]autorelease];

UIImage *sendImage = [UIImage imageNamed:@"orangebuttonsmall.png"];
UIButton *Sendbutton = [UIButton buttonWithType:UIButtonTypeCustom];
Sendbutton.frame = CGRectMake(0, 0,50,25);
[Sendbutton setTitle:@"Send" forState:UIControlStateNormal];

[Sendbutton setBackgroundImage:sendImage forState:UIControlStateNormal];
Sendbutton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
Sendbutton.titleLabel.font = [UIFont systemFontOfSize:10];
Sendbutton.titleLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5]; 
Sendbutton.titleLabel.shadowOffset = CGSizeMake(0, -1);
Sendbutton.bounds = CGRectMake( 0, 0, 50, 25 );   
Sendbutton.titleLabel.font = [UIFont boldSystemFontOfSize:13];

[Sendbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[Sendbutton addTarget:self action:@selector(pickerDoneClicked:) forControlEvents:UIControlEventTouchUpInside];    
UIBarButtonItem *doneButton = [[[UIBarButtonItem alloc ] initWithCustomView:Sendbutton]autorelease];

[self.toolBar setItems:[NSArray arrayWithObjects:numButton,flex, doneButton, nil]];
[self.view addSubview:self.toolBar];

self.txtView = [[UITextView alloc] init];
self.txtView.layer.cornerRadius = 15;;
self.txtView.clipsToBounds = YES;
self.txtView.frame = CGRectMake(45, 100, 274, 98);
self.txtView.layer.borderWidth = 1.0f;
self.txtView.delegate = self;
self.txtView.scrollEnabled = YES;
self.txtView.backgroundColor = [UIColor whiteColor];
self.txtView.contentInset = UIEdgeInsetsZero;
self.txtView.returnKeyType = UIReturnKeyDone;  // type of the return key
self.txtView.layer.borderColor = [[UIColor blueColor] CGColor];
self.txtView.font = [UIFont fontWithName:@"Helvetica" size:17.0f];
[self.txtView setInputAccessoryView:self.toolBar];
[self.view addSubview:self.txtView];

self.itemsArray = [[[NSArray alloc]initWithObjects:@"William Langland",@"Philip Larkin", @"Dorianne Laux", @"David Lehman", @"Denise Levertov", nil]autorelease];

-(void)showPicker

[self.txtView resignFirstResponder];

self.pickerView = [[[UIPickerView alloc]init]autorelease];
self.pickerView.showsSelectionIndicator = YES;
self.pickerView.dataSource = self;
self.pickerView.delegate = self;
self.pickerView.tag = 0;
self.pickerView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
                                        UIViewAutoresizingFlexibleHeight);
[self.pickerView sizeToFit];

self.pickerView.frame = CGRectMake(0, 293 , 320, 15);

self.toolBar.frame = CGRectMake(0,253, 320, 35);

[self.view addSubview:self.toolBar];
[self.view addSubview:self.pickerView]; 
[self.pickerView selectRow:1 inComponent:0 animated:YES];

【问题讨论】:

【参考方案1】:

试试这个链接

http://blog.swierczynski.net/2010/12/how-to-create-uipickerview-with-toolbar-above-it-in-ios/

它展示了如何在 uipicker 上放置一个 inputAccessoryView

【讨论】:

我已经检查了你的代码..但是 UIPickerView 没有“setInputAccessoryView”方法..请让我知道你对此的建议..

以上是关于添加与 inputAccessaryView 相同的工具栏时不显示 UIToolBar的主要内容,如果未能解决你的问题,请参考以下文章

您将如何使用与参数的类名相同的名称添加时间偏移

如何在opencv中为图像添加边框,边框颜色必须与图像颜色相同

如何在 ag-grid 中添加与单元格中的数据相同的默认 headerTooltip?

是否可以添加检查所有子模块是否具有与父 pom 中定义的相同版本?

将“活动”类添加到与当前单击的侧边栏菜单链接相同的 href

还有其他使用Hue的方法吗----不是必须添加Linux用户名与Hue用户名相同吗?