将UIBarButtonItem移动几个像素?

时间:2012-06-13 10:19:37

标签: objective-c xcode uinavigationbar uibarbuttonitem backbarbuttonitem

我创建了一个比Apples默认导航栏高一点的自定义UINavigationBar

我似乎无法找到一种方法将UIBarButtonItem向下移动,直接在两条虚线之间居中。

有一种简单的方法吗?我已经尝试创建自定义按钮但没有成功。理想情况下,我只想将默认的后退按钮向下移动几个像素。

enter image description here

用于创建UINavigationBar,自定义标题图片和UIBarButtonItem的代码:

    //Create image for navigation background
UIImage *NavigationPortraitBackground = [[UIImage imageNamed:@"navbackground.png"]
                                         resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UINavigationBar appearance] setBackgroundImage: NavigationPortraitBackground
                                   forBarMetrics:UIBarMetricsDefault];

//Centered title image
UIImageView *headerImage = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"hometitle.png"]];
    [headerImage setFrame: CGRectMake(0, 0, 180, 49)];
self.navigationItem.titleView = headerImage;

UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] init];
[backBarButtonItem setTintColor: [UIColor colorWithRed:0 green:69.0/255 blue:118.0/255 alpha:1]];
[backBarButtonItem setStyle: UIBarButtonItemStylePlain];
    self.navigationItem.backBarButtonItem = backBarButtonItem;
[backBarButtonItem release];

提前致谢,

2 个答案:

答案 0 :(得分:3)

使用自定义视图创建UIBarButtonItem。这个自定义视图将是一个UIView,其实际UIButton(作为子视图)从顶部放置x像素(x =您要将其向下移动的像素数)。

答案 1 :(得分:-1)

抱歉

UIButton *myButton1 = [UIButton buttonWithType:UIButtonTypeCustom];  
 [myButton1 setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];   myButton1.showsTouchWhenHighlighted = YES;  
 myButton1.frame = CGRectMake(0.0, 3.0, 50,30);  
  [myButton1 addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:myButton1];    
 self.navigationItem.leftBarButtonItem = leftButton;