2012-02-07 47 views
7

我已将UIToolBarUIBarButtonItem作为inputAccessoryView添加为UITextView。它工作正常,但UIBarButtonItem可以在它的框架之外触摸,也许在右边外面有50个像素。这没什么大不了的,但它让我很烦恼。有人知道为什么iOS - UIToolBar作为UITextView的inputAccessoryView

这是我的代码(ARC):

UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 44)]; 
toolBar.barStyle = UIBarStyleBlack; 
toolBar.translucent = YES; 

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneWriting:)]; 
[toolBar setItems:[NSArray arrayWithObjects:doneButton, nil]]; 

self.messageTextView.inputAccessoryView = toolBar; 

回答

2

工具栏,似乎如果有工具栏上没有其他的按键附近的扩大超出边界按钮的活动区域。苹果公司的工程师必须认为,试图猜测用户想要按什么位置而不是根本不反应会更好。

+0

如果是这样的话,对我来说,感觉更像是“马虎”的编程,让按钮在其界限之外作出反应,哦。 – 2012-02-24 13:24:35

7

在iOS 6中,它似乎表现得像预期的那样。 尼斯提示:如果您想按钮出现,而不是左右,请使用下列之一:

[toolBar setItems:[NSArray arrayWithObjects:flexibleSpace, doneButton, nil]]; 
2

我希望它可以帮助你:

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

然后用初始化工具栏。 ..

UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init]; 
[keyboardDoneButtonView sizeToFit]; 

UIBarButtonItem* PrevButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:105 target:nil action:nil]; //< 
UIBarButtonItem* NextButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:106 target:nil action:nil]; //> 
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(doneClicked:)]; 

UIBarButtonItem* flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
UIBarButtonItem *fake = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil] ; 

[keyboardDoneButtonView setItems:[NSArray arrayWithObjects: PrevButton,fake, NextButton,fake,flexSpace,fake,doneButton,nil] animated:YES]; 

使用项目上的按钮进行得确切夹点位置...

+0

非常有趣的解决方案,这一个为我工作,谢谢。 – carlos16196 2016-10-22 10:16:53