注意:我可以通过在我的barbutton和barbutton2方法中将leftbarbuttonitem切换为rightbarbutton项来使其工作。我只是想知道为什么它不能正常工作!

出于某种奇怪的原因,我的leftbarbuttonitem正在显示,但是我的rightbarbuttonitem从未弹出!这是我的代码

    -(void)barButton {
    image = [UIImage imageNamed:@"BBut.png"];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage: [image stretchableImageWithLeftCapWidth:7.0 topCapHeight:0.0] forState:UIControlStateNormal];
    [button setBackgroundImage: [[UIImage imageNamed: @"BBut.png"] stretchableImageWithLeftCapWidth:7.0 topCapHeight:0.0] forState:UIControlStateHighlighted];

    [button addTarget:self action:@selector(showlocations:) forControlEvents:UIControlEventTouchUpInside];
    button.frame= CGRectMake(3.0, 0.0, image.size.width+1, image.size.height+0);

    UIView *v=[[UIView alloc] initWithFrame:CGRectMake(3.0, 0.0, image.size.width+1, image.size.height) ];

    [v addSubview:button];

    UIBarButtonItem *modal = [[UIBarButtonItem alloc] initWithCustomView:v];
    self.navigation.leftBarButtonItem = modal;


-(void)barButton2 {
    image2 = [UIImage imageNamed:@"Refresh.png"];

    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button2 setBackgroundImage: [image2 stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0] forState:UIControlStateNormal];
    [button2 setBackgroundImage: [[UIImage imageNamed: @"Refresh.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0] forState:UIControlStateHighlighted];
        // [button2 addTarget:self action:@selector(viewDidLoad) forControlEvents:UIControlEventTouchUpInside];
    button2.frame= CGRectMake(281.0, 5.0, image2.size.width, image2.size.height);

    UIView *v2=[[UIView alloc] initWithFrame:CGRectMake(281.0, 5.0, image2.size.width, image2.size.height) ];

    [v2 addSubview:button2];
        UIBarButtonItem *refresh = [[UIBarButtonItem alloc] initWithCustomView:v2];    self.navigation.rightBarButtonItem = refresh;
    NSLog(@"THE ACTION HAS BEEN RECIEVED!"); }

在我的ViewDidLoad方法中,我有这个:
[self barButton2];
[self barButton];

该操作确实完成,并且我确实收到了我的NSLog方法。有人知道为什么会这样吗?左栏按钮始终显示,如果我将第一个栏按钮更改为rightbarbuttonitem并将barbutton2更改为leftbarbutton项,然后更改CGRect坐标,则它会起作用,这就是我现在正在使用的功能,但是为什么会这样呢?提前致谢。

最佳答案

您看不到正确的按钮,因为它是在屏幕外部绘制的。
button2 X坐标设置为281.0,其父视图X坐标也设置为281.0。结果,您的按钮位于x = 562中,这意味着它在屏幕之外。

button2 X坐标设置为0,看看是否有帮助:

button2.frame= CGRectMake(0.0, 5.0, image2.size.width, image2.size.height);

10-08 06:04