将UIToolbar与两个UIBarButtonItem添加到UINavigationBar:poor UIToolbar和iPhone 4 [英] Adding UIToolbar with two UIBarButtonItem to a UINavigationBar: poor UIToolbar and what about iPhone 4

查看:124
本文介绍了将UIToolbar与两个UIBarButtonItem添加到UINavigationBar:poor UIToolbar和iPhone 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要关注这里的第二个提示。在这个提示中,两个UIBarButtonItems放在一起在一个UIToolbar。最后,UIToolbar被添加到UINavigationBar。现在对我的问题:



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

  UIToolbar * toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0,90,44.01)]; 

如何摆脱白线?请参阅:



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



2)iPhone 3和iPhone 4的显示尺寸的区别是什么?



$ b

b

按照下面的示例创建按钮,从上面提到的网站:

  //创建工具栏在右边有两个按钮
UIToolbar * tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,133,44.01)];

//创建用于保存按钮的数组,然后将其添加到工具栏
NSMutableArray * buttons = [[NSMutableArray alloc] initWithCapacity:3];

//创建一个标准的添加按钮
UIBarButtonItem * bi = [[UIBarButtonItem alloc]
initWithButtonSystemItem:UIBarButtonSystemItemAdd target:self action:NULL];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
[bi release];

//创建一个分隔符
bi = [[UIBarButtonItem alloc]
initWithButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[buttons addObject:bi];
[bi release];

//创建一个标准的刷新按钮
bi = [[UIBarButtonItem alloc]
initWithButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh :)];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
[bi release];

//粘贴工具栏中的按钮
[tools setItems:buttons animated:NO];

[buttons release];

//并将工具栏放在导航栏中
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];

@ tc。:



到子类 UIToolbar

  // MyToolbar.h 

#import< Foundation / Foundation.h>


@interface MyToolbar:UIToolbar {

}

@end

// MyToolbar.m

#importMyToolbar.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


解决方案>

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



你可能能够子类化UIToolbar,使它不绘制(即覆盖-drawRect:做任何事情)。


I'm following the second tip from here. In this tip two UIBarButtonItems are put together in a UIToolbar. Finally, the UIToolbar is added to the UINavigationBar. Now to my problems:

1) A white line is on top of the UIToolbar. If I increase the size of the UIToolbar, the gradient is wrong. I'm using the following size for the UIToolbar:

    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 90, 44.01)];

How can I get rid of the white line? See here:

The problem is that there is a white instead of a grey line. If it would be grey, everything would be perfect.

2) What about the difference of the display size of iPhone 3 and iPhone 4? Do I have to check which iPhone is used and then double the size?

Edit:

The buttons are created like in the following example I took from the above mentioned website:

// 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.:

I tried to subclass 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

解决方案

There's no guarantee that UIToolbar draws seamlessly inside a UINavigationBar; this might be responsible for the white line you're seeing.

You might be able to subclass UIToolbar so that it doesn't draw (i.e. override -drawRect: to not do anything).

这篇关于将UIToolbar与两个UIBarButtonItem添加到UINavigationBar:poor UIToolbar和iPhone 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆