iOS 5后退button大小

我正在使用iOS 5 UIAppearance协议来使用自定义的导航栏后退button。 不像其他方法,我发现,这是最好的,因为它保留了默认的后退buttonanimation和行为。

唯一的问题是,我不能改变它的大小或设置它不剪辑子视图 。 这是发生了什么事情:

剪切图像

A是预期行为,B是默认样式,C是剪辑结果。

不幸的是,这不像设置UIBarButtonItem到clipsToBounds = NO那么简单。

有谁知道如何解决这个问题? 谢谢!

正如你发现的, UIAppearance代理不会让你调整button本身的尺寸: UIBarButtonItem支持的外观方法列表在文档中给出,虽然它包括标题标签本身的指标,限制。

对于它的价值,button本身实际上是44点(88像素,如果你在视网膜)触摸区域高,只是buttongraphics比这小。

如果你已经设置了高达3磅的高度差,那么最好的select可能是创build你自己的自定义button,并使用UINavigationItem setLeftBarButtonItem方法。 正如你所指出的,你将需要实现一个简短的方法附加到该button,实现popNavigationController以确保正确的行为仍然存在。

更新 :我刚刚发现,如果你保持你的后退button,你可以,实际上,它的animation,它有一个类似的方式滑到一个标准的后退button。 在下面的代码中, self.backButton是您在initWithCustomView UIBarButtonItem方法中使用的UIButton

 - (void)viewWillDisappear:(BOOL)animated { [UIView animateWithDuration:0.3 animations:^{ self.backButton.frame = CGRectMake(self.backButton.frame.origin.x+100, self.backButton.frame.origin.y, self.backButton.frame.size.width, self.backButton.frame.size.height); }]; } 

…这将触发每当视图控制器消失(即,当popup推),但你可以挂钩到UINavigationController委托,只有当导航控制器popup,而不是触发它。 当控制器被按下时,它肯定会移动button,虽然我只在模拟器上testing过它。

我结束了对我在导航控制器中使用的每个VC使用自定义视图控制器类。

OEViewController.h

 #import <UIKit/UIKit.h> #import "OEBarButtonItem.h" @interface OEViewController : UIViewController @property (nonatomic, retain) UIButton *backButton; @property (atomic) BOOL pushed; - (void)pushViewController:(UIViewController*)vc; @end 

OEViewController.m

 #import "OEViewController.h" #define ANIMATION_DURATION 0.33 @implementation OEViewController @synthesize backButton, pushed; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. pushed=YES; self.navigationItem.hidesBackButton = YES; backButton = [OEBarButtonItem backButtonWithTitle:[(UIViewController*)[self.navigationController.viewControllers objectAtIndex:[self.navigationController.viewControllers count]-2] title]]; [backButton addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton]; } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; if (pushed) { self.backButton.frame = CGRectMake(self.backButton.frame.origin.x+100, self.backButton.frame.origin.y+6, self.backButton.frame.size.width, self.backButton.frame.size.height); [UIView animateWithDuration:ANIMATION_DURATION animations:^{ self.backButton.frame = CGRectMake(self.backButton.frame.origin.x-100, self.backButton.frame.origin.y, self.backButton.frame.size.width, self.backButton.frame.size.height); }]; pushed=NO; } else { self.backButton.frame = CGRectMake(self.backButton.frame.origin.x-100, self.backButton.frame.origin.y, self.backButton.frame.size.width, self.backButton.frame.size.height); [UIView animateWithDuration:ANIMATION_DURATION animations:^{ self.backButton.frame = CGRectMake(self.backButton.frame.origin.x+100, self.backButton.frame.origin.y, self.backButton.frame.size.width, self.backButton.frame.size.height); }]; } } - (void)backButtonPressed:(id)sender { [self.navigationController popViewControllerAnimated:YES]; [UIView animateWithDuration:ANIMATION_DURATION animations:^{ self.backButton.frame = CGRectMake(self.backButton.frame.origin.x+100, self.backButton.frame.origin.y, self.backButton.frame.size.width, self.backButton.frame.size.height); }]; } - (void)pushViewController:(UIViewController*)vc { [self.navigationController pushViewController:vc animated:YES]; [UIView animateWithDuration:ANIMATION_DURATION animations:^{ self.backButton.frame = CGRectMake(self.backButton.frame.origin.x-100, self.backButton.frame.origin.y, self.backButton.frame.size.width, self.backButton.frame.size.height); }]; } @end 

与使用UIAppearance相比,这具有使用iOS 4.0+的额外好处。

感谢@lxt的帮助!