1

ナビゲーション バーの高さを変更し、左上隅にカスタム イメージ ボタンを追加する必要があります。私は途中ですが、カスタム画像ボタンを正しい位置に配置することに迷っています。ここに私が持っているものがあります:

高さを調整するために、次の 1 つのメソッドで UINavBar カテゴリを作成しました。

- (CGSize)sizeThatFits:(CGSize)size {
    CGSize newSize = CGSizeMake(768,80);
    return newSize;
}

@end

ボタンを変更する UINavigationController サブクラスも作成しました。そのクラスの viewDidLoad は次のとおりです。

UIImage *navBackgroundImage = [UIImage imageNamed:@"bar"];
[[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];


// Change the appearance of back button
UIImage *backButtonImage = [[UIImage imageNamed:@"back_off"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 13, 0, 6)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

// Change the appearance of other navigation button
UIImage *barButtonImage = [[UIImage imageNamed:@"menu_off"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 6, 0, 6)];
[[UIBarButtonItem appearance] setBackgroundImage:barButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

これまでのところ、このソリューションは上部のナビゲーション バーのサイズを変更しますが、ボタンを奇妙な位置に配置します。これが私が望むものと何が起こっているかです:

私が欲しいもの

ゴール

私が得るもの

実際

4

1 に答える 1

1

オフセット プロパティを持つ UIBarButtonItem カテゴリを使用しています。

UIBarButtonItem+CustomImage.h

@interface UIBarButtonItem (CustomImage)

+ (UIBarButtonItem*)barItemWithImage:(UIImage*)image target:(id)target action:(SEL)action offset:(CGPoint)offset;

@end

UIBarButtonItem+CustomImage.m

#import "UIBarButtonItem+CustomImage.h"

@implementation UIBarButtonItem (CustomImage)

+ (UIBarButtonItem *)barItemWithImage:(UIImage *)image target:(id)target action:(SEL)action offset:(CGPoint)offset {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:image forState:UIControlStateNormal];
    [button setFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height)];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [button setBounds:CGRectOffset(button.bounds, 0.0, -10.0)];

    UIView *container = [[UIView alloc] initWithFrame:button.frame];
    [container setBounds:CGRectOffset(container.bounds, offset.x, offset.y)];
    [container addSubview:button];

    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:container];
    return item;
}

@end

使用例

#import "UIBarButtonItem+CustomImage.h"

UIBarButtonItem *settingsButton = [UIBarButtonItem barItemWithImage:settingsImage
                                                 target:self
                                                 action:@selector(revealSettings:)
                                                 offset:CGPointMake(0.0, 0.0)];

[self.navigationItem setLeftBarButtonItem:settingsButton];
于 2013-11-19T20:06:58.277 に答える