【问题标题】:Custom right side navigation bar button自定义右侧导航栏按钮
【发布时间】:2012-09-13 14:46:05
【问题描述】:

我在情节提要的 tableView 中添加了一个右栏按钮,创建了一个打开另一个窗口的 IB 操作。一切正常。但现在我想为该按钮使用自定义图像。我发现了这个:

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:infoIconImage forState:UIControlStateNormal];

我猜多加几行代码就可以完成这项工作, 但我似乎无法理解该按钮在代码中的名称。我如何将故事板中的那个按钮与代码联系起来。

初学者问题,抱歉。

【问题讨论】:

    标签: ios uibutton uinavigationbar


    【解决方案1】:

    创建一个 IBOutlet,就像您将故事板中的操作链接到 .h 文件一样。将其命名为 button,您就可以开始了!

    顺便说一句,您可以在情节提要中设置“标准”图像:选择按钮,转到 Xcode 实用工具栏中的属性检查器(右侧栏),然后更改标识符!

    编辑:

    看看那里使用你自己的图像:

    Customize UIBarButtonItem

    关于 UIBarButtonItem 的部分在文末。

    【讨论】:

    • 谢谢,这回答了我的问题,但又提出了另一个问题。它创建了一个 UIBarButtonItem 类型。任何想法如何自定义它(使用自定义图像)?上面的代码 sn-p 处理的是 UIButton...
    • 查看我的自定义图像更新,以防您发布的解决方案不起作用:)
    • 我在下面发布的代码运行良好,自定义按钮在那里,但由于某种原因,该操作不再起作用......
    【解决方案2】:

    好的,我发现有一个属性customView。所以这里是:

        // Set the custom back button
    UIImage *infoIconImage = [UIImage imageNamed:@"info-button.png"];
    //create the button and assign the image
    UIButton *iButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [iButton setImage:infoIconImage forState:UIControlStateNormal];
    
    //set the frame of the button to the size of the image
    iButton.frame = CGRectMake(0, 0, infoIconImage.size.width, infoIconImage.size.height);
    
    //assigning customized button
    infoButton.customView = iButton;
    

    【讨论】:

    • 有谁知道为什么当我将 iButton 分配给我在情节提要中创建的 infoButton 时,推送操作被禁用?我怀疑 UIControlStateNormal。
    • 不是UIControlStateNormal,因为这只是告诉按钮你传递的图像(infoIconImage)是normal状态的按钮图像.所以我认为没问题..如果我想到一个想法,我会告诉你..
    猜你喜欢
    • 2014-10-01
    • 2014-02-27
    • 1970-01-01
    • 2017-12-30
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    相关资源
    最近更新 更多