【问题标题】:Programmatically created UIButtonBarItem not picking up UIAppearance以编程方式创建的 UIButtonBarItem 没有拾取 UIAppearance
【发布时间】:2013-04-13 14:18:02
【问题描述】:

我在我的 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;

}

【问题讨论】:

    标签: ios uiappearance


    【解决方案1】:

    试试这样:

    [UIBarButtonItem apperanceWhenContainedIn: [UINavigationController 类],nil] 设置背景图像:bimg forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

    【讨论】:

    • 谢谢。做到了。我试过 setBackButtonBackgroundImage 但似乎 setBackgroundImage 成功了,因为这些按钮在技术上不是后退按钮,因为它们是在开始时。
    猜你喜欢
    • 2012-07-03
    • 1970-01-01
    • 1970-01-01
    • 2017-10-30
    • 1970-01-01
    • 1970-01-01
    • 2019-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多