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

时间:2012-01-30 10:08:19

标签: iphone objective-c uipickerview uitoolbar

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

我希望在uikeyboard的顶部有uittoolbar。我已经添加了uitoolbar控件作为InputAccessoryView。 当我这样做时,uitoolbar附带uikeyboard。这很好。

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

所以当点击按钮时,我已经取消了ui键盘。 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];
}

Here the toolbar is shown using inputAccessaryView

Here the toolbar is not shown

1 个答案:

答案 0 :(得分:0)

试试此链接

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

它显示了如何将inputAccessoryView放在uipicker