调整大小会导致横向中奇怪的

调整大小会导致横向中奇怪的

本文介绍了UIToolbar 调整大小会导致横向中奇怪的 UIBarButtonItem 大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我创建并添加 UIToolBar 和 UIBarButton 项目时,如何让工具栏和项目在两个方向都正确显示.这是我目前所拥有的:

When I create and add a UIToolBar and UIBarButton items, how can I get the toolbar and items to appear properly in both orientations. This is what I have so far:

self.toolbar = [[[UIToolbar alloc]initWithFrame:CGRectMake(0, 372, 320, 44)]autorelease];
self.toolbar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

self.deleteButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(deleteButtonClicked)] autorelease];
self.deleteButton.style = UIBarButtonItemStylePlain;

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

self.shareButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(shareButtonClicked)] autorelease];
self.shareButton.style = UIBarButtonItemStylePlain;
self.navigationItem.leftBarButtonItem = backItem;

NSArray *toolbarItems = [NSArray arrayWithObjects:self.shareButton,flexibleSpace,self.deleteButton,nil];
[self.toolbar setItems:toolbarItems animated:NO];

从图片中可以看出,UIBarButtonItems 在纵向上看起来是正确的,但在横向上却奇怪地被压扁和偏离中心(尤其是垃圾桶图标).是否有我遗漏的明显解决方案,或者我是否必须手动调整大小以使其正确?

As you can see from the images, the UIBarButtonItems look correct in portrait, but weirdly squished and off center (especially the trash icon), in landscape. Is there an obvious solution I'm missing, or do I have to do manual resizing to get this right?

这是一个微妙的区别,但它应该是这样的:

It's a subtle difference, but here's what it should look like:

推荐答案

至少在 iOS 5 之前,答案是使用 UINavigationController 的内置工具栏,而不是创建自己的工具栏.内置工具栏将正确自动调整大小.

The answer, at least until iOS 5 is to use the UINavigationController's built in toolbar instead of creating your own. The built in toolbar will resize correctly and automatically.

这篇关于UIToolbar 调整大小会导致横向中奇怪的 UIBarButtonItem 大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 21:30