我想要得到一个宽度UIBarButtonItem.
这不起作用:
barbuttonitem.customView.frame.size.width
Run Code Online (Sandbox Code Playgroud)
这也行不通:
barbuttonitem.width
Run Code Online (Sandbox Code Playgroud) 我正在附加一个UIToolbar我UITextView的inputAccessoryView,为了添加一个按钮来解除键盘.这种方法效果很好,当设备处于纵向模式时看起来很正确.但是,当设备处于横向模式时,我无法确定如何将工具栏的大小调整为用于工具栏的较低高度.
我在文本视图的委托-textViewShouldBeginEditing:方法中添加了工具栏:
if (!textView.inputAccessoryView) {
UIToolbar *keyboardBar = [[UIToolbar alloc] init];
keyboardBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
keyboardBar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissKeyboard:)];
[keyboardBar setItems:[NSArray arrayWithObjects:spaceItem, doneButton, nil]];
[spaceItem release];
[doneButton release];
[keyboardBar sizeToFit];
textView.inputAccessoryView = keyboardBar;
[keyboardBar release];
}
Run Code Online (Sandbox Code Playgroud)
不过,我在横向模式下从这段代码中得到了奇怪的行为.如果我在横向模式下开始编辑,则工具栏具有横向高度,但"完成"按钮从屏幕上拉出一半.如果我然后旋转到纵向模式,完成按钮将被绘制在正确的位置,当我旋转回横向模式时它仍然保持在正确的位置.
如果我在纵向模式下开始编辑,工具栏将以纵向高度绘制,但"完成"按钮将绘制在正确的位置.如果我然后旋转到横向模式,工具栏将保持纵向高度,但"完成"按钮仍至少绘制在正确的位置.
有关如何在设备旋转时调整大小的建议吗?我真的希望有一种更自动的方式,而不是在一个视图控制器的旋转事件中手动插入高度幻数.