8

在 iPhone 应用程序中,我们可以使用以下代码制作 UIBarButtonItem:

UIBarButtonItem *bbix=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:nil action:nil];

生成的 UIBarButtonItem 有一个系统提供的“动作”图标。我的问题是:如何获取这个 UIBarButtonItem 的内部 UIButton 并将这个 UIButton 添加到另一个 UIView?谁能给我看一些代码?

4

7 回答 7

12

你不能。UIBarButtonItem继承自UIBarItem继承自NSObject。这些类都没有获取 a 的方法UIButton,因为UIBarButtonItemis not a UIButton

相反,您可以UIBarButtonItem从 a创建一个UIButton- 请参阅我为不同问题提供的答案以获取更多信息(通过将其添加为“自定义视图”来工作)。

于 2010-06-13T08:11:29.403 回答
6

访问栏按钮项的“customView”属性是一种可能的解决方案:

UIBarButtonItem *item = (UIBarButtonItem *)[self.navigationItem.rightBarButtonItems objectAtIndex:0];
                     OR
UIBarButtonItem *item = (UIBarButtonItem *)[self.navigationItem.rightBarButtonItem];

UIButton *myBtn;

if([item.customView isKindOfClass:[UIButton class]]) 
{ 
    myBtn = (UIButton*)item.customView;
}

if(myBtn)
{
    // do something
}

试试这个。它真的对我有用:)

于 2014-09-20T03:54:34.923 回答
3

我认为无论如何您都无法获得嵌入式按钮。但是,如果您使用自定义视图创建按钮,例如使用按钮,我们可以稍后使用以下代码获取该按钮:

((UIButton *)(theBarButtonItem.customView.subviews.lastObject));

自定义视图的层次结构应该是这样的:

|--UIView
  |--UIButton
  |--UIImageView

希望有帮助。

于 2013-07-02T17:46:24.207 回答
1

您无法通过 UIBarButtonItem 访问 UIBarButtonItem 内的嵌入式按钮

您可以做什么来使用界面构建器将内部按钮与新插座链接起来,这样您可以直接访问该按钮您可以在打开 nib 文件时在对象视图中找到该按钮,然后您可以将其与新出口

于 2013-08-07T10:25:44.957 回答
0

如果您通过 initWithCustomView 创建了条形按钮项,只需访问自定义视图属性并转换为 UIButton。

http://cocoatouch.blog138.fc2.com/blog-entry-214.html

于 2014-04-23T14:56:05.637 回答
0

如果您的 UIBarButton 是用自定义视图制作的,只是为了扩展 Brians 的答案......

if ([myBarButton.customView.subviews.lastObject isKindOfClass:[UIButton class]])
{
    UIButton * btn = ((UIButton *)(myBarButton.customView.subviews.lastObject));
}

只需添加一点错误检查,您永远不知道苹果或其他开发人员何时会更改布局层次结构。因为从长远来看,这种方法真的很容易出错。

于 2014-03-28T15:48:43.777 回答
0

这是一个精确复制使用系统类型UIBarButtonItem获取的图像的解决方案。UIBarButtonSystemItemAction例如,新创建的 UIButton 被插入到MKAnnotationView

创建一个包含此方法的类别文件:

@implementation UIImage (Custom)

+ (UIImage *)actionButtonImage
{
    CGRect rect = CGRectMake(0, 0, 20, 27);

    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);

    [[UIColor colorWithRed:3/255.0 green:122/255.0 blue:1 alpha:1] set];

    UIBezierPath *path = [UIBezierPath bezierPath];

    // Box
    [path moveToPoint:CGPointMake(7, 8)];
    [path addLineToPoint:CGPointMake(1, 8)];
    [path addLineToPoint:CGPointMake(1, 26)];
    [path addLineToPoint:CGPointMake(19, 26)];
    [path addLineToPoint:CGPointMake(19, 8)];
    [path addLineToPoint:CGPointMake(13, 8)];

    // Arrow shaft
    [path moveToPoint:CGPointMake(10, 17)];
    [path addLineToPoint:CGPointMake(10, 1)];

    // Arrow head
    [path moveToPoint:CGPointMake(6, 4.5)];
    [path addLineToPoint:CGPointMake(10, 0.5)];
    [path addLineToPoint:CGPointMake(14, 4.5)];

    [path stroke];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

@end

MKMapView委托中,添加此实现(根据需要进行调整):

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    MKPinAnnotationView *view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Item"];
    view.canShowCallout = YES;

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *actionImage = [UIImage actionButtonImage];
    [button setImage:actionImage forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, actionImage.size.width, actionImage.size.height);
    view.leftCalloutAccessoryView = button;

    return view;
}
于 2015-08-03T11:32:14.533 回答