2

ここで見られるように、iOS 5 UIAppearance プロトコルを使用して、カスタム ナビゲーション バーの戻るボタンを使用しています。私が見つけた他の方法とは異なり、これはデフォルトの戻るボタンのアニメーションと動作を保持するため、最適です。

唯一の問題は、サイズを変更したり、サブビューをクリップしないように設定したりできないことです。何が起こっているかは次のとおりです。

クリッピング画像

A は意図した動作、B はデフォルト スタイル、C はクリップされた結果です。

残念ながら、UIBarButtonItem を clipsToBounds=NO に設定するほど簡単ではありません。

この問題を解決する方法を知っている人はいますか?ありがとう!

4

2 に答える 2

5

お気づきのとおり、UIAppearanceプロキシではボタン自体のサイズを調整できません。サポートされている外観方法のリストはUIBarButtonItemドキュメントに記載されており、タイトル ラベル自体のメトリックが含まれていますが、ボタンは立ち入り禁止です。

価値があるのは、ボタン自体は実際にはタッチ可能な領域の点で44ポイント(網膜の場合は88ピクセル)の高さであり、ボタンのグラフィックがそれよりも小さいということです.

高さの差が 3 pt に相当するものに固執している場合は、独自のカスタム ボタンを作成し、その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代わりにナビゲーション コントローラーがポップされたときにのみトリガーすることができます。シミュレーターでしかテストしていませんが、コントローラーを押すとボタンが動くように見えます。

于 2012-05-06T15:07:15.643 に答える
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

これには、UIAppearance を使用するのではなく、iOS 4.0 以降で動作するという追加の利点があります。

@lxt の助けに感謝します!

于 2012-05-08T02:34:09.820 に答える