2016-05-03 8 views
0

右の大きいボタンとして大きなプラスボタンを使用しようとしていますが、以下のコードはストーリーボード内で動作しましたが、すべてをxibファイルに移動して.swiftファイルを分離した後、壊れたのは唯一のものでした。UINavigationBarにカスタムUIBarButtonItemを追加する

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はxibファイルを持たない独自の.swiftファイルです。

なぜそれがストーリーボードで機能したのか分かりませんが、今は理解できません。 (通常のフォントサイズが少し小さすぎるようだと)

理想的には、私は

+0

あなたはXIBファイルに何を入れたのですか?ナビゲーションバー? – MudOnTire

+0

ナビゲーションコントローラには別のビューコントローラが含まれていますが、xibファイルはありません。 https://github.com/gomeow/BillSplitter/blob/master/BillSplitter/GroupNavViewController.swift#L20-L26 –

答えて

1

私のために働きます。私はターゲットアクションを取り除いたので、実装する必要はありません。すべてコードで完了しました。ストーリーボードも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) 

} 
+0

これは、ありがとう、働いた。私はナビゲーションコントローラにボタンを追加しようとしていました。ビューコントローラには含まれていませんでした。 –

+0

それを聞いてうれしいです。答えをplzとしてマークする – ha100

1

UINavigationControllerはなしXIBファイルを持つ独自の.swiftファイルで大きなプラスボタンを持っているしたいと思います。

UINavigationControllerではなく、UIViewControllerクラスでこのボタンを作成します。同じコードをUIViewControllerに移動します。

+0

それは動作しませんでした、タイトルはそのように表示されません –

関連する問題