2012-02-07 22 views
7

Tôi đã thêm UIToolBar với UIBarButtonItem làm inputAccessoryView cho số UITextView. Nó hoạt động tốt nhưng UIBarButtonItem có thể chạm vào bên ngoài khung của nó, có lẽ 50 pixel bên ngoài bên phải. Nó không có vấn đề gì lớn nhưng nó làm tôi khó chịu. Có ai biết tại sao không?iOS - UIToolBar dưới dạng inputAccessoryView cho UITextView

Đây là mã của tôi (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; 

Trả lời

2

Thanh công cụ dường như mở rộng khu vực hoạt động của các nút xa hơn giới hạn của họ nếu không có các nút lân cận khác trên thanh công cụ. Các kỹ sư của Apple phải nghĩ rằng nó là tốt hơn để cố gắng đoán nơi người dùng có ý định nhấn chứ không phải là không phản ứng gì cả.

+0

Nếu trường hợp đó xảy ra, đối với tôi, nó giống như lập trình "cẩu thả" có nút phản ứng bên ngoài giới hạn của nó, tốt thôi. –

7

Trong iOS 6 có vẻ như ứng xử như mong đợi. đẹp Mẹo: Nếu bạn muốn nút xuất hiện ở phía bên phải thay vì bên trái, sử dụng một trong những:

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

Sau đó khởi thanh công cụ với:

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

Tôi hy vọng nó sẽ giúp bạn. ..

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

Sử dụng Fake hàng để có được vị trí chính xác trên nhúm nút ...

+0

giải pháp rất thú vị, điều này làm việc cho tôi, cảm ơn. – carlos16196

Các vấn đề liên quan