Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 更改已完成的UIBAButtonim';背景图像_Iphone_Uiimage_Uibarbuttonitem - Fatal编程技术网

Iphone 更改已完成的UIBAButtonim';背景图像

Iphone 更改已完成的UIBAButtonim';背景图像,iphone,uiimage,uibarbuttonitem,Iphone,Uiimage,Uibarbuttonitem,我想制作一个自定义UIBarButtonItem来表示“完成”按钮。 我希望能够国际化的应用程序,所以我希望文本可以直接编辑。为了考虑“完成”标签的不同长度,我设计了一个可拉伸图像。是否可以直接更改默认的编辑按钮背景,或者是否需要自定义UIBarButtonim?如果是这样,根据标签的长度动态调整背景拉伸图像大小的方法是什么?如果您仅针对iOS 5.0,您可以使用新的UIAppearance方法更改默认外观,特别是-setBackButtonBackgroundImage:forState:ba

我想制作一个自定义UIBarButtonItem来表示“完成”按钮。
我希望能够国际化的应用程序,所以我希望文本可以直接编辑。为了考虑“完成”标签的不同长度,我设计了一个可拉伸图像。是否可以直接更改默认的编辑按钮背景,或者是否需要自定义UIBarButtonim?如果是这样,根据标签的长度动态调整背景拉伸图像大小的方法是什么?

如果您仅针对iOS 5.0,您可以使用新的
UIAppearance
方法更改默认外观,特别是
-setBackButtonBackgroundImage:forState:barMetrics:

如果您需要支持较旧版本的iOS,您应该为
UIBarButtonItem
子类,添加一个
UIButton
实例变量,创建它并在
UIBarButtonItem
init
方法中调用
-initWithCustomView:
。这是因为
uibarbuttonim
不是
UIView
的子类,您无法在其中绘制自定义图像。您还应该手动设置
UIBarButtonItem
width
属性

@interface MYCustomBarButtonItem : UIBarButtonItem
{
    UIButton *button;
}
- (id)initWithTitle:(NSString *)title; // name it in concordance with your needs
@end

#define kButtonHeight 30
#define kButtonTitleFontSize 12
#define kButtonTitlePadding 5

@implementation MYCustomBarButtonItem

- (id)initWithTitle:(NSString *)title
{
    button = [UIButton buttonWithType:UIButtonTypeCustom]; // add retain here, and write the dealloc method if you aren't using ARC. Also, release it if self == nil.
    self = [super initWithCustomView:button];
    if (self) {
        UIFont *titleFont = [UIFont boldSystemFontOfSize:kButtonTitleFontSize];
        CGSize titleSize = [title sizeWithFont:titleFont];
        CGFloat buttonWidth = titleSize.width + kButtonTitlePadding * 2;
        button.frame = CGRectMake(0, 0, buttonWidth, kButtonHeight);
        self.width = buttonWidth;

        [button setTitle:title forState:UIControlStateNormal];

        // Set your styles
        button.titleLabel.font = titleFont;

        // Normal state background
        UIImage *backgroundImage = ...; // create your normal stretchable background image
        [button setBackgroundImage:backgroundImage forState:UIControlStateNormal];

        // Pressed state background
        backgroundImage = ...; // create your pressed stretchable background image
        [button setBackgroundImage:backgroundImage forState:UIControlStateHighlighted];

        // and so on...
    }
    return self;
}

@end

注意:不要忘记覆盖子类的
目标
操作
属性,以便使用
按钮
实例。

如果您仅针对iOS 5.0,可以使用新的
UIAppearance
方法更改默认外观,
-setBackButtonBackgroundImage:forState:barMetrics:
特别是

如果您需要支持较旧版本的iOS,您应该为
UIBarButtonItem
子类,添加一个
UIButton
实例变量,创建它并在
UIBarButtonItem
init
方法中调用
-initWithCustomView:
。这是因为
uibarbuttonim
不是
UIView
的子类,您无法在其中绘制自定义图像。您还应该手动设置
UIBarButtonItem
width
属性

@interface MYCustomBarButtonItem : UIBarButtonItem
{
    UIButton *button;
}
- (id)initWithTitle:(NSString *)title; // name it in concordance with your needs
@end

#define kButtonHeight 30
#define kButtonTitleFontSize 12
#define kButtonTitlePadding 5

@implementation MYCustomBarButtonItem

- (id)initWithTitle:(NSString *)title
{
    button = [UIButton buttonWithType:UIButtonTypeCustom]; // add retain here, and write the dealloc method if you aren't using ARC. Also, release it if self == nil.
    self = [super initWithCustomView:button];
    if (self) {
        UIFont *titleFont = [UIFont boldSystemFontOfSize:kButtonTitleFontSize];
        CGSize titleSize = [title sizeWithFont:titleFont];
        CGFloat buttonWidth = titleSize.width + kButtonTitlePadding * 2;
        button.frame = CGRectMake(0, 0, buttonWidth, kButtonHeight);
        self.width = buttonWidth;

        [button setTitle:title forState:UIControlStateNormal];

        // Set your styles
        button.titleLabel.font = titleFont;

        // Normal state background
        UIImage *backgroundImage = ...; // create your normal stretchable background image
        [button setBackgroundImage:backgroundImage forState:UIControlStateNormal];

        // Pressed state background
        backgroundImage = ...; // create your pressed stretchable background image
        [button setBackgroundImage:backgroundImage forState:UIControlStateHighlighted];

        // and so on...
    }
    return self;
}

@end

注意:不要忘记覆盖子类的
目标
操作
属性,以便使用
按钮
实例。

我可能误解了你的问题,但我相信你想要的是:

UIBarButtonItem *btnDone = [[UIBarButtonItem alloc] initWithTitle:@"I am done" style:UIBarButtonItemStyleBordered target:self action:nil];
UIImage *stretchable = [[UIImage imageNamed:@"StretchableImage.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:16];
[btnDone setBackgroundImage:stretchable forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.navigationItem setRightBarButtonItem:btnDone];

uibarbuttonite会自动将其宽度调整为标签的宽度。

我可能误解了你的问题,但我相信你想要的是:

UIBarButtonItem *btnDone = [[UIBarButtonItem alloc] initWithTitle:@"I am done" style:UIBarButtonItemStyleBordered target:self action:nil];
UIImage *stretchable = [[UIImage imageNamed:@"StretchableImage.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:16];
[btnDone setBackgroundImage:stretchable forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.navigationItem setRightBarButtonItem:btnDone];

UIBarButtonItem会自动将其宽度调整为标签的宽度。

@Stanislav Yaglo是正确的。此解决方案仅适用于iOS 5。若你们想针对旧版本,你们需要制作一个简单的UIBarButtonItem子类。@Stanislav Yaglo是对的。此解决方案仅适用于iOS 5。如果你想以旧版本为目标,你需要为UIBarButtonItem创建一个简单的子类。如何根据文本的宽度调整按钮的大小。另外,如何将背景设置为可拉伸图像?谢谢。您使用的是
NSString
-sizeWithFont:
方法。背景是用
ui按钮设置的
-setBackgroundImage:forState:
。如何根据文本的宽度调整按钮的大小。另外,如何将背景设置为可拉伸图像?谢谢。您使用的是
NSString
-sizeWithFont:
方法。背景用
ui按钮
-setBackgroundImage:forState:
设置。