ios - 如何在自身类中设置 UIBarButtonSystemItem (Swift)

标签 ios xcode swift

我为条形按钮项创建了一个类,以便在所有 view controllers

中使用它
import UIKit

class SearchBarItem: UIBarButtonItem {
    let searchBarItemImage = UIBarButtonSystemItem.Search

    required convenience init?(coder aDecoder: NSCoder) {
        self.init()
        self.title = "AnyText" /// This one working and Title will changed

    }

}

我需要将 SearchBarItem 图像设置为 UIBarButtonSystemItem.Search

我应该是这样的:

self.barButtonSystemItem = UIBarButtonSystemItem.Search

------------------------让我解释一下

我想创建一个类来设置我项目中所有 View Controller 的“条形按钮项目”的身份检查器。

此类应将考试更改图像的“条形按钮项目”自定义为 UIBarButtonSystemItem.Search。

我做了一些 SearchBarItem: UIBarButtonItem 类,我可以用这个方便的初始化更改“Bar Button Item”的标题:

required convenience init?(coder aDecoder: NSCoder) {
    self.init()
    self.title = "AnyText"
}

但这不是我需要的。我需要将图像更改为放大。所以我搜索并发现放大图像在 UIBarButtonSystemItem 枚举中,带有 .Search

我的问题是:如何在 SearchBarItem 类的初始化中设置 UIBarButtonSystemItem.Search

最佳答案

我找到了另一种方法: 我创建了一个类来控制 Navigation Items,这是我的解决方案:

import UIKit

class NavigationItems: UINavigationItem {

    convenience required init?(coder: NSCoder) {
        self.init()
        let searchBtn = UIBarButtonItem.init(barButtonSystemItem: .Search, target: nil, action: "SearchNavClick")
        self.rightBarButtonItem = searchBtn
    }

}

并将导航项标识检查器类更改为这个新类 (NavigationItems)

关于ios - 如何在自身类中设置 UIBarButtonSystemItem (Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35791862/

相关文章:

iphone - 使用 UIImageWriteToSavedPhotosAlbum 保存图像时无法识别的选择器错误

ios - 如何通过 segue Swift 4 将数据从 View Controller 的 UITableViewCell 类传递到另一个 ViewController?

ios - Swift 打印 anchor 常量值

ios - iOS 项目中的 Firebase 导致代码覆盖率下降

ios - 更新子字符串(到 :) results in generic parameter could not be inferred

iphone - 在 ViewController ("Page swipe Style ScrollView"中收到内存警告)

iOS 应用程序在手机上崩溃,但在模拟器上运行正常

ios - mac mini 4gb 用于开发 IOS 移动应用程序(如社交网络等..)

swift - SpriteKit : why node in collision has category bit mask of 4294967295 when this category was never assigned to a node

ios - 如果 .srt 文件包含多行字幕文本,如何在 swift 中使用 Scanner 解析它?