【问题标题】:UIBarButtonItem - changing background and removing textUIBarButtonItem - 改变背景和删除文本
【发布时间】:2014-11-27 11:29:44
【问题描述】:

我正在尝试更改导航栏中 presentViewController 中的 完成 按钮,我成功地能够使用此更改背景:

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nav_bg.png"] forBarMetrics:UIBarMetricsDefault];

现在,当将“完成”按钮替换为图像时,图像会重复,并且“完成”文本会保留在那里。使用这个:

UIImage *doneBackgroundImage = [[UIImage imageNamed:@"back-home.png"]
                                            resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

            [[UIBarButtonItem appearance] setBackgroundImage:doneBackgroundImage
                                                    forState:UIControlStateNormal
                                                       style:UIBarButtonItemStyleDone
                                                  barMetrics:UIBarMetricsDefault];

这是它的样子:

请问我在这里做错了什么?

【问题讨论】:

    标签: ios xcode uinavigationbar uibarbuttonitem


    【解决方案1】:

    步骤:1

    创建一个 UIButton 并在您的 presentViewContoller 中使用 initWithCustomView 将其分配给 UIBarButtomItem

    UIImage *customImage = [UIImage imageNamed:@"yourCustomImage"];
    UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0, customImage.size.width, customImage.size.height);
    [backButton setImage:[UIImage imageNamed:@"yourCustomImage"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(showPreviousView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc]initWithCustomView:customButton];
    

    步骤:2

    你可以从这个SO POST找到更多

     viewController.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourimage.png"]];
        UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourimage2.jpg"]]];    
        viewController.navigationItem.rightBarButtonItem = item; 
    

    【讨论】:

    • 抱歉,没有一个解决方案对我有用。我在iOS8上。我只需要在预览 PDF 文件时简单地从导航栏中删除 DONE 文本并将其替换为主页图像。
    • 好的,我可以使用:NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: [UIColor clearColor], UITextAttributeTextColor, [UIColor clearColor], UITextAttributeTextShadowColor, nil]; [[UIBarButtonItem appearance] setTitleTextAttributes: attributes forState: UIControlStateNormal]; 清除 DONE 文本,但我如何不重复图像
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多