将带有两个UIBarButtonim的UIToolbar添加到UINavigationBar:糟糕的UIToolbar和iPhone4呢

将带有两个UIBarButtonim的UIToolbar添加到UINavigationBar:糟糕的UIToolbar和iPhone4呢,iphone,objective-c,cocoa-touch,uinavigationbar,uitoolbar,Iphone,Objective C,Cocoa Touch,Uinavigationbar,Uitoolbar,我正在遵循来自的第二个提示。在本技巧中,两个UIBarButtonim放在一个UIToolbar中。最后,UIToolbar被添加到UINavigationBar。现在谈谈我的问题: 1) UIToolbar顶部有一条白线。如果我增加UIToolbar的大小,渐变是错误的。我对UIToolbar使用以下大小: UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 90, 44.01)]; 我怎样才能

我正在遵循来自的第二个提示。在本技巧中,两个UIBarButtonim放在一个UIToolbar中。最后,UIToolbar被添加到UINavigationBar。现在谈谈我的问题:

1) UIToolbar顶部有一条白线。如果我增加UIToolbar的大小,渐变是错误的。我对UIToolbar使用以下大小:

    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 90, 44.01)];
我怎样才能摆脱白线?请看这里:

问题是有一条白线而不是灰线。如果它是灰色的,一切都会很完美

2) iPhone3和iPhone4的显示尺寸有什么不同?我是否需要检查使用的是哪一款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
//MyToolbar.h
#进口
@界面MyToolbar:UIToolbar{
}
@结束
//MyToolbar.m
#导入“MyToolbar.h”
@实现我的工具栏
-(void)drawRect:(CGRect)rect{
//无所事事
}
-(id)initWithFrame:(CGRect)aRect{
if((self=[super initWithFrame:aRect])){
self.不透明=否;
self.backgroundColor=[UIColor clearColor];
self.clearsContextBeforeDrawing=是;
}
回归自我;
}
@结束
1)我无法解释这条白线。奇怪的是,它只在你的按钮上方。按钮是如何创建的?另外,是否有理由将高度设置为44.01,而不仅仅是44?我不能肯定你设定的高度在任何情况下都是值得尊重的,他们可能会被迫达到44(如果我错了,有人会纠正我)


2) 你不必为iPhone4做任何事情,一切都是自动缩放的

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


您可能可以将UIToolbar子类化,使其不会绘制(即覆盖-drawRect:不执行任何操作)。

您是否尝试在容器
UIView
中添加这些按钮而不是,然后将其作为项目添加,但使用
UIBarButtonSystemFlexibleSpace
进行操作?我的意思是将这些按钮作为一个独立的项目添加。

一个UIToolbar设计用于Iphone屏幕的底部,因此如果您在其他地方使用,它将尝试在顶部产生边缘效果。为了避免这种情况,工具栏高度应该比导航栏高度大2个像素。Bu这一次,您将有一个不同的副作用,它会导致导航栏底部的边缘清晰。(在任何情况下,导航栏都会将右按钮项定位为在中间对齐)

是的,我没有使用
UIView
,也没有将它们作为独立项添加。相反,我正在创建一个
UIToolbar
,创建一个按钮数组,将按钮添加到工具栏,最后将工具栏添加到导航栏。有关详细信息,请参阅我编辑的问题。白线仅位于我的按钮上方,因为我将按钮放在UIToolbar中。然后我把这个UIToolbar放在UINavigationBar上。因此,白线来自我的UIToolbar的大小/图纸。是的,44.01或44都很重要。44.01的高度非常合适(除了这条白线)。如果我拿44,那么我会得到更多的绘画作品。我也可以拿50作为高度。然后白线消失,但渐变不再与导航栏相同。有关创建此工件的详细信息,请参阅我编辑的问题。我尝试将
UIToolbar
子类化。现在我的工具栏有了一个黑色的背景。我必须实现哪些函数才能从
UINavigationBar
.Hmmm获得相同的渐变/样式。尝试在
initWithFrame:
initWithCoder:
中另外设置
self.obaque=NO
self.backgroundColor=[UIColor clearColor]
、和
self.ClearContextBeforeDrawing=YES
。现在我已经覆盖了
initWithFrame:
(请参阅我编辑的问题)。乍一看,这似乎奏效了。我以前从未对UI元素进行过子类化。我是否必须知道某些事情(不起作用的事情或类似的事情)?您必须知道UIToolbar是如何绘制的。它似乎没有被记录,所以你几乎不得不猜测。它可能会在未来的操作系统版本中发生变化。但是,UIToolbar文档不会告诉您不要将其子类化,因此您相当安全。