如何在导航栏中移动条形按钮项目

时间:2012-05-17 10:36:10

标签: iphone objective-c ios uinavigationcontroller

我有一个自定义栏按钮项目,我想轻推5个像素。我将如何使用以下代码执行此操作。我试图设置“y”点,但不断出现语法错误。

     [self.navigationController setNavigationBarHidden:NO animated:YES];

     UIImage *image = [UIImage imageNamed:@"arrow_back.png"];
     UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

     [self.navigationController.navigationBar addSubview:imageView];

    [imageView release];

     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
     [button setImage:buttonImage forState:UIControlStateNormal];

     //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;

    [customBarItem release];
}

感谢您的帮助

1 个答案:

答案 0 :(得分:3)

UIBarButton项目不会从UIView继承,因此没有可以直接调整的“frame”属性,因此您需要使用不同的方法。

尝试:

    [self.navigationItem.leftBarButtonItem setBackgroundVerticalPositionAdjustment:5 forBarMetrics:UIBarMetricsDefault];