BackButton Stretchable

时间:2012-05-19 20:00:18

标签: objective-c xcode cocoa ios5

我想要一个可伸缩的后退按钮我现在正在使用这一行编码,这是给我一个后退按钮,但我希​​望它可伸缩自定义标题,所以我试图在代码中添加这一行

[button setTitle:@"back" forState:UIControlStateNormal];

但它不起作用

   UIImage *buttonImage = [[UIImage imageNamed:@"backButton.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 16, 0, 16)];

    //create the button and assign the image
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:buttonImage  forState:UIControlStateNormal];

    //buttonImage.size.width
    //set the frame of the button to the size of the image (see note below)
    button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);

    [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];

    //create a UIBarButtonItem with the button as a custom view
    UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = customBarItem;

在NavigationBar中显示我需要一个标题,以便它可以拉伸?

http://i.imgur.com/Cylbz.png

1 个答案:

答案 0 :(得分:2)

如果您希望图片可以拉伸,则必须使用setBackgroundImage:forState代替setImage:forState:

您还应该将按钮的宽度设置为更高的值,以使您的标题适合。由于图像是可伸缩的,因此按钮的大小不必与图像相同。