苹果手机 。 UIToolbar无法使用某些按钮

时间:2011-11-15 12:00:52

标签: iphone ios cocoa-touch ipad

我有一个UIToolbar。

如果我像这样创建一个UIBarButton,它可以工作:

UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:iOB style:UIBarButtonItemStylePlain target:self action:@selector(myStuff)];
[button setTitle:@"Hi"];

如果我像这样创建UIBarButton,因为我希望它被着色:

UIImageView *vUP = [[UIImageView alloc] initWithImage:iUP];
UIBarButtonItem *UPButton = [[UIBarButtonItem alloc] initWithCustomView:vUP];
[UPButton setAction:@selector(anotherStuff)];
[UPButton setTitle:@"Hi";
[UPButton setTarget:self];
[vUP release];

按钮显示,没有标题,不会响应任何触摸......

我错过了什么吗?

2 个答案:

答案 0 :(得分:1)

使用UIButton而不是UIImageView,因为UIImageView不响应触摸事件。 在任何情况下,您都不会像提供自定义视图那样获得标题。

答案 1 :(得分:1)

UIImage* image3 = [UIImage imageNamed:@"plain_btn-75-30.png"];
CGRect frameimg = CGRectMake(0, 0, image3.size.width, image3.size.height);

UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];   
    [someButton setBackgroundImage:image3 forState:UIControlStateNormal];
    [someButton addTarget:self action:@selector(backButtonPress:)forControlEvents:UIControlEventTouchUpInside];
    [someButton setShowsTouchWhenHighlighted:YES];
    [someButton setTitle:@"Back" forState:UIControlStateNormal];
    someButton.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:13];

    [someButton setTitleColor:[UIColor blackColor]  forState:UIControlStateNormal];

     UPButton =[[UIBarButtonItem alloc] initWithCustomView:someButton];

如果您想要图像和标题,则必须将图像设置为带标题的背景图像。