在UINavigationBar中添加带有两个UIBarButtonItem的UIToolbar:UIToolbar质量差,iPhone 4怎么办?

8
我正在遵循这里的第二个提示。在此提示中,将两个UIBarButtonItems放在UIToolbar中。最后,将UIToolbar添加到UINavigationBar中。现在是我的问题:
1)在UIToolbar的顶部有一条白线。如果我增加UIToolbar的大小,则渐变会出现问题。我正在使用以下大小的UIToolbar:
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 90, 44.01)];

如何去掉白线?请看这里:alt text 问题是白线应该是灰色的,如果是灰色的话就完美了。
2)iPhone 3和iPhone 4的显示尺寸有什么区别?我需要检查使用的是哪种iPhone,然后将大小加倍吗?
编辑:
按钮的创建方式如下面从上述网站中提取的示例一样:
// create a toolbar to have two buttons in the right
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 133, 44.01)];

// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];

// create a standard "add" button
UIBarButtonItem* bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:NULL];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
[bi release];

// create a spacer
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[buttons addObject:bi];
[bi release];

// create a standard "refresh" button
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh:)];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
[bi release];

// stick the buttons in the toolbar
[tools setItems:buttons animated:NO];

[buttons release];

// and put the toolbar in the nav bar
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];

@ tc.:

我尝试对UIToolbar进行子类化。

// MyToolbar.h

#import <Foundation/Foundation.h>


@interface MyToolbar : UIToolbar {

}

@end

// MyToolbar.m

#import "MyToolbar.h"


@implementation MyToolbar

- (void)drawRect:(CGRect)rect {
    // do nothing
}

- (id)initWithFrame:(CGRect)aRect {
    if ((self = [super initWithFrame:aRect])) {
        self.opaque = NO;
        self.backgroundColor = [UIColor clearColor];
        self.clearsContextBeforeDrawing = YES;      
    }
    return self;
}

@end
4个回答

5

不能保证UIToolbar在UINavigationBar内无缝绘制,这可能是你看到的白线的原因。

你可以尝试子类化UIToolbar,使其不绘制(即重写-drawRect:以不执行任何操作)。


我尝试子类化UIToolbar。现在我的工具栏有黑色背景。我需要实现哪些函数才能获得与UINavigationBar相同的渐变/样式? - testing
2
嗯,尝试在 initWithFrame:initWithCoder: 中适当设置 self.opaque = NOself.backgroundColor = [UIColor clearColor]self.clearsContextBeforeDrawing = YES - tc.
现在我已经重写了initWithFrame:方法(请看我的编辑过的问题)。乍一看似乎可以工作。我以前从未子类化过UI元素。我需要注意什么(哪些东西不能用或类似的东西)吗? - testing
你必须了解UIToolbar如何进行绘制。它似乎没有被记录在文档中,所以你基本上只能猜测。它可能会在未来的操作系统版本中发生变化。然而,UIToolbar文档并没有告诉你不要对其进行子类化,因此你是相对安全的。 - tc.

0

1) 我无法解释这条白线。奇怪的是它只在你的按钮上方。按钮是如何创建的?此外,您为什么将高度设置为44.01而不是44呢?我不能确定您设置的高度在任何情况下都会被采纳,它们可能被强制为44(如果我错了,请有人纠正我)。

2) 对于iPhone 4,您无需做任何事情,一切都会自动缩放。


白线只出现在我的按钮上方,因为我将我的按钮放在了UIToolbar中。然后我将这个UIToolbar放在了UINavigationBar上。所以白线来自于我的UIToolbar的大小/绘制。是的,如果是44.01或44,确实很重要。44.01的高度效果非常好(除了这条白线)。如果我选择44,则会出现更多的绘图伪影。我也可以选择50作为高度。然后白线就消失了,但渐变不再与导航栏相同。请参见我编辑后的问题,了解有关创建此伪影的详细信息。 - testing

0

你尝试过将这些按钮放在一个UIView容器中,然后将其作为项目添加,但使用UIBarButtonSystemItemFlexibleSpace进行操作吗?我的意思是将每个按钮作为独立的项目添加。


是的,我没有使用UIView,也没有将它们添加为独立项目。相反,我创建了一个UIToolbar,创建了一个按钮数组,将按钮添加到工具栏,最后将工具栏添加到导航栏。请参阅我的编辑问题以获取详细信息。 - testing

0

UIToolbar被设计用于iPhone屏幕底部,因此如果在其他地方使用它,将尝试在顶部制造边缘效果。为避免这种情况,工具栏高度应比Navbar高度大2像素。但是这次您将产生不同的副作用,导致导航栏的底部出现清晰的边缘。(无论如何,在任何情况下,导航栏都将将rightBarButtonItem设置为居中对齐)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接