Reputation: 605
I am trying to have a large plus button as a right bar button, the code below worked while it was in a storyboard, but after moving everything to xib files and separate .swift files, this was the only thing that broke.
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)
The UINavigationController is its own .swift file with no xib file.
I can't figure out why it worked in the storyboard, but now it doesn't.
Ideally, I'd like to have a large plus button (as the normal font size seems a bit too small)
Upvotes: 0
Views: 1031
Reputation: 1592
does work for me. i just got rid of target action so i don't have to implement it. all done in code. no storyboards and no xibs.
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)
}
Upvotes: 1
Reputation: 1935
The UINavigationController is its own .swift file with no xib file.
Make this Button in the UIViewController class and not in your UINavigationController. So move the same code to your UIViewController.
Upvotes: 1