gpt4 book ai didi

ios - 将自定义 UIBarButtonItem 添加到 UINavigationBar

转载 作者:行者123 更新时间:2023-11-28 12:49:25 25 4
gpt4 key购买 nike

我正在尝试将一个大的加号按钮作为右栏按钮,下面的代码在 Storyboard中时有效,但是在将所有内容移动到 xib 文件和单独的 .swift 文件之后,这是唯一失败的东西。

let button = UIButton(type: .Custom)
button.setTitle("+", forState: .Normal)
button.titleLabel?.font = UIFont.systemFontOfSize(20.0)
button.frame = CGRectMake(0, 0, 100, 40)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "+", style: .Plain, target: self, action: #selector(GroupNavViewController.onCreate))
self.navigationItem.rightBarButtonItem?.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFontOfSize(40)], forState: .Normal)

UINavigationController 是它自己的 .swift 文件,没有 xib 文件。

我不明白为什么它在 Storyboard 中起作用,但现在不起作用。

理想情况下,我想要一个大的加号按钮(因为正常的字体大小似乎有点太小了)

最佳答案

对我有用。我刚刚摆脱了目标行动,所以我不必实现它。全部用代码完成。没有 Storyboard,也没有 xib。

AppDelegate.swift

var window: UIWindow?
let viewController = ViewController();

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

let navigationController = UINavigationController(rootViewController:viewController)

self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window!.backgroundColor = UIColor.whiteColor()
self.window!.rootViewController = navigationController
self.window!.makeKeyAndVisible()

return true
}

ViewController.swift

override func viewDidLoad() {
super.viewDidLoad()

let button = UIButton(type: .Custom)
button.setTitle("+", forState: .Normal)
button.titleLabel?.font = UIFont.systemFontOfSize(20.0)
button.frame = CGRectMake(0, 0, 100, 40)

self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "+", style: .Plain, target: self, action: nil)
self.navigationItem.rightBarButtonItem?.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFontOfSize(40)], forState: .Normal)

}

关于ios - 将自定义 UIBarButtonItem 添加到 UINavigationBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36994040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com