0

我在我的 appdelegate 中添加了 UIAppearance 代码来自定义 UIButtonBarItem。导航控制器中的后退按钮获取 UI 外观。但是我以编程方式创建的 UIButtonBarItems 出现在第一个导航栏上并没有获得 uiappearance。UIAppearance 不应该适用于所有创建的 UIButtonBarItems 吗?如果我将 UIButtonBarItem 放入情节提要的导航栏中,也会发生同样的事情。导航栏拾取 UIAppearance 但 UIButtonBarItem 没有。

在应用程序中

-(void) configureAppearance
{
    // navbar background
    UIView* bk0 = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 480.0f, 44.0f)];
    UIView* bk1 = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 480.0f, 22.0f)];
    UIView* bk2 = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 22.0f, 480.0f, 22.0f)];
    [bk0 addSubview:bk1];
    [bk0 addSubview:bk2];
    bk1.backgroundColor=LIGHT_PURPLE;
    bk2.backgroundColor=DARK_PURPLE;
    UIImage* navimg=ChangeViewToImage(bk0);
    [[UINavigationBar appearance] setBackgroundImage:navimg forBarMetrics:UIBarMetricsDefault];

#define BUTTON_SIZE 34.0f
    UIView* bbk0 = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, BUTTON_SIZE, BUTTON_SIZE)];
    UIView* bbk1 = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, BUTTON_SIZE, BUTTON_SIZE)];
    bbk0.backgroundColor=[UIColor clearColor];
    bbk1.backgroundColor=DARK_PURPLE;

    [bbk0 addSubview:bbk1];
    addBorderAndShadow(bbk1, VERY_DARK_PURPLE, 1.0, 2.0);
    UIImage* bimg=ChangeViewToImage(bbk0);

    bimg = [bimg resizableImageWithCapInsets:UIEdgeInsetsMake(0.0f, BUTTON_SIZE, 0, 0)]; // this will keep it square

    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:bimg
                                                      forState:UIControlStateNormal
                                                    barMetrics:UIBarMetricsDefault];
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, bimg.size.height*2) forBarMetrics:UIBarMetricsDefault]; // to not show text on button but still get nav ani

    //[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setBackButtonBackgroundImage:bimg forState:UIControlStateNormal  barMetrics:UIBarMetricsDefault];

}

在我的一个视图控制器中(没有 uiappearance

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;


    //AppDelegate* appdel = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    //[appdel configureAppearance];

    UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self
                                                                  action:@selector(revealMenu:)];
    self.navigationItem.leftBarButtonItem = backButton;

}
4

1 回答 1

1

试试这样:

[UIBarButtonItem apperanceWhenContainedIn: [UINavigationController class],nil] setBackgroundImage:bimg forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

于 2013-04-13T15:07:13.760 回答