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

发布于 2024-12-29 11:55:38 字数 5366 浏览 3 评论 0原文

我有一个 uipickerview ,按下 uibutton 时将填充该视图。 这个uipickerview在uipickerview的顶部有一个uitoolbar控件。 该工具栏上有 uibuttons。它工作正常,直到我执行以下操作。

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

但是当我点击按钮时,它必须显示带有该工具栏的 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];
}

此处使用 inputAccessaryView 显示工具栏

此处不显示工具栏

I have got a uipickerview which will be populated when pressing a uibutton.
This uipickerview has a uitoolbar control in the top of the uipickerview.
That tool bar has got uibuttons in it.It is working fine until i do the below thing.

I want to have that uittoolbar, on top of the uikeyboard.So I have added , that uitoolbar control as InputAccessoryView.
When i did this, the uitoolbar comes with uikeyboard.that is fine.

but when i tap on the button, which has to show the uipickerview with that toolbar and also needs to resign the toolbar.

So when tap on the button, i have resigned the uikeyboard.
the uipickerview is showing but the toolbar is not visible.

I have been trying to fix this for a long time, but really facing a tough time.

Please let me know.

- (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];
}

Here the toolbar is shown using inputAccessaryView

Here the toolbar is not shown

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸩远一方 2025-01-05 11:55:38

试试这个链接

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

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

Try this link

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

It shows how to put a inputAccessoryView on uipicker

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文