以编程方式创建UIButtonBarItem而不是拾取UIAppearance

时间:2013-04-13 14:18:02

标签: ios uiappearance

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

在appdelegate中

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

}

在我的一个viewcontrollers中(没有得到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;

}

1 个答案:

答案 0 :(得分:1)

试试这样:

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