2012-05-06 121 views
2

我正在使用iOS 5 UIAppearance protocol来使用自定义导航栏后退按钮,如图所示here。与我发现的其他方法不同,这是最好的,因为它保留了默认的后退按钮动画和行为。iOS 5后退按钮尺寸

唯一的问题是,我不能更改其大小或将其设置为不剪辑子视图。这里发生的事情:

Clipping Image

一个是预期的行为,B是默认的样式,C是修剪结果。

不幸的是,它不像设置UIBarButtonItem到clipsToBounds = NO那么容易。

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

+0

你有没有试过UIBarButtonItem.superview.clipsToBounds = NO? (或superview.superview ...) 因为导航栏并不打算比它已经大。所以任何你会尝试的将是一个'黑客'。如果它是黑客,那么这是'标准'的方法:-) –

+0

你推荐的是不可能的,因为'UIBarButtonItem'不能从'UIView'继承。不过,我试着设置'self.navigationController.navigationItem.backBarButtonItem.customView.clipsToBounds = NO;'和'self.navigationItem.backBarButtonItem.customView.clipsToBounds = NO;'。我假设你的意思是'UIBarButtonItem'不打算更大,因为我没有触摸导航栏本身。 – jpsim

+0

是的你是对的!我通常所说的是遍历视图层次结构并找出哪一个剪辑。您也可能需要照顾iOS < 5 and iOS > = 5。 其实我的意思是导航栏并不意味着更大,因为酒吧项目不受限制,导航栏是限制它们的东西。它有硬编码的填充,剩下的就是按钮的空间。 –

回答

5

正如您发现的那样,UIAppearance代理不会让您调整按钮本身的尺寸:UIBarButtonItem支持的外观方法列表在文档中给出,并且它包含标题标签本身的指标,按钮是禁止的。

对于它的价值,按钮本身实际上是44分(88个像素,如果你在视网膜是)高的可触摸面积而言,它只是按钮图形比小。

如果你已经死了,那么最好的选择可能是创建你自己的自定义按钮并使用UINavigationItemsetLeftBarButtonItem方法。正如你指出的那样,你将需要实现一个简短的方法附加到该按钮,实现popNavigationController,以确保正确的行为依然存在。

更新:我刚刚发现,如果你保持你的后退按钮,事实上,它可以使它滑动到与标准后退按钮类似的方式。在下面的代码self.backButtonUIButton您可能会在您的initWithCustomViewUIBarButtonItem方法中使用。

- (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); 
        }]; 
} 

...每当视图控制器中消失,这将触发(即弹出当推),但你可以钩到UINavigationController委托只触发它时,导航控制器,而不是弹出。当控制器被按下时,它肯定会移动按钮,尽管我只在模拟器上进行了测试。

+0

感谢您确认我对'UIAppearance',@lxt的怀疑。不幸的是,通过设置一个自定义'leftBarButtonItem',我失去了'backBarButtonItem'动画(水平滑动),因为'UIBarButtonItem'不能从'UIView'继承,所以我不能自己动画。 – jpsim

+0

试试我刚发布的代码,可能会帮助你动画。似乎为我工作。 – lxt

2

我结束了对我在导航控制器中使用的每个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 

这有与iOS 4.0+的工作,而不是使用UIAppearance的好处。

感谢@lxt的帮助!