0
的
我是新來的迅速,在一個推送通知的點擊,我試圖打開一個VC,這裏是代碼添加一個UIButton到的導航欄RootViewController的
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main",bundle: nil)
let detailVC = mainStoryboard.instantiateViewControllerWithIdentifier("DetailsViewController") as! DetailsViewController
detailVC.eventId = eventId
let navigationController = UINavigationController(rootViewController:detailVC)
let myButton = UIButton()
myButton.titleLabel!.frame = CGRectMake(15, 54, 300, 500)
myButton.titleLabel!.text = "Button Label"
myButton.titleLabel!.textColor = UIColor.redColor()
myButton.titleLabel!.textAlignment = .Center
navigationController.navigationBar.addSubview(myButton)
self.window?.rootViewController = navigationController
現在用VC的導航欄頂部打開,但我沒有看到添加任何按鈕。所以我想知道我錯在哪裏。
基於@Hugo REPONSE
我更新viewDidLoad中的代碼
let btnName = UIButton()
//btnName.setImage(UIImage(named: "imagename"), forState: .Normal)
btnName.titleLabel?.text = "Test"
btnName.titleLabel?.textColor = UIColor.whiteColor()
btnName.frame = CGRectMake(0, 30, 30, 30)
btnName.addTarget(self, action: Selector("action"), forControlEvents: .TouchUpInside)
//.... Set Right/Left Bar Button item
let rightBarButton = UIBarButtonItem()
rightBarButton.customView = btnName
self.navigationItem.rightBarButtonItem = rightBarButton
但仍沒有運氣
問候 蘭吉特
您好@Hugo Tunius感謝。我試過你的邏輯,但我沒有看到任何按鈕 – Ranjit
你不能使用'UIButton'需要'UIBarButtonItem' –
或者很好的customView可能實際上工作 –