-3

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

[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

4

1 回答 1

2

如果您希望图像可拉伸,则必须使用setBackgroundImage:forState而不是。setImage:forState:

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

于 2012-05-19T20:41:45.923 回答