-->

iOS 5的后退按钮大小(iOS 5 Back Button Size)

2019-07-29 06:31发布

我使用的是iOS 5的UIAppearance协议使用自定义导航栏后退按钮,当看到这里 。 不像我找到其他方法,这个人是最好的,因为它保留了默认的后退按钮动画和行为。

唯一的问题是,我不能改变它的大小或将其设置为不夹子视图 。 这里是发生了什么:

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

不幸的是,它不是的UIBarButtonItem设置为clipsToBounds = NO一样简单。

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

Answer 1:

正如你已经发现, UIAppearance代理不会让你调节按钮本身的尺寸:支持的外观方法列表UIBarButtonItem的文档中给出,并同时它包括标题标签本身的度量按钮被摘限制。

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

如果你是一个相当于在高度为3磅差那么最好的选择很可能将是创建自己的自定义按钮,使用死心塌地UINavigationItem setLeftBarButtonItem方法。 正如你指出,你将需要实现连接到该按钮,实现短的方法popNavigationController ,以确保正确的行为仍然存在。

更新 :我刚刚发现,如果你保持你的背部按钮,你身边可以,事实上,动画它有它以类似的方式滑动到一个标准的后退按钮。 在下面的代码self.backButtonUIButton ,你会在你使用了initWithCustomView UIBarButtonItem方法。

- (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委托只触发它时,导航控制器,而不是弹出。 它肯定似乎当控制器推动按钮,虽然我只测试了它在模拟器上。



Answer 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他的帮助!



文章来源: iOS 5 Back Button Size