【问题标题】:change image of UIBarButton更改 UIBarButton 的图像
【发布时间】:2017-12-07 08:08:34
【问题描述】:

我的目标是以编程方式更改 UIBarButton(我用作 IBOutlet)的图像。

我在 stackoverflow change-image-of-uibutton-with-other-image 上找到了这个解决方案。但这不适用于 iOS 4.2。

谁能帮帮我。 西蒙

我试过了:

UIImage *image = [UIImage imageWithContentsOfFile: 
                 [[NSBundle mainBundle] pathForResource:@"play" ofType:@"png"]];
playButton.image = image;

UIImage *image = [UIImage imageWithContentsOfFile:
                 [[NSBundle mainBundle] pathForResource:@"play" ofType:@"png"]];UIImage *image = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"play" ofType:@"png"]];
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
UIButton* someButton = [[UIButton alloc] initWithFrame:frame];
[someButton setBackgroundImage:image forState:UIControlStateNormal];
[someButton setShowsTouchWhenHighlighted:YES];
playButton = [[UIBarButtonItem alloc]initWithCustomView:someButton];

[someButton release];

【问题讨论】:

    标签: iphone image ios4 uibarbuttonitem


    【解决方案1】:

    如果您想将图像放到按钮上,请使用以下代码。

    UIImage *image = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"play" ofType:@"png"]];
    
    CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
    
    UIButton* someButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [someButton setBackgroundImage:image forState:UIControlStateNormal];
    [someButton setShowsTouchWhenHighlighted:YES];
    playButton = [[UIBarButtonItem alloc]initWithCustomView:someButton];
    

    【讨论】:

    • 更短的加载 UIImage 的方法UIImage *image = [UIImage imageNamed:@"play.png"];
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-07
    • 2019-12-04
    • 2018-05-22
    • 1970-01-01
    • 1970-01-01
    • 2011-03-19
    • 1970-01-01
    相关资源
    最近更新 更多