外部设置UINavigation 的 Title 和 barbuttonItem

功能简单,不想新写个控制器,直接设置navigation的标题和返回按钮,可以用此方法设置

let contro = UIViewController()
@IBAction func goAgreement(sender: AnyObject) {
        let webView = UIWebView(frame: UIScreen.mainScreen().bounds)
        webView.loadRequest(NSURLRequest(URL: NSBundle.mainBundle().URLForResource("agreement", withExtension: "html")!))
        
        contro.view.addSubview(webView)
        
        let navi = UINavigationController(rootViewController: contro)
        navi.navigationBar.topItem!.title = "用户协议"
        navi.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.redColor()]
        navi.navigationBar.topItem?.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "icon_back")?.imageWithRenderingMode(.AlwaysOriginal), style: .Plain, target: self, action: #selector(LoginViewController.backToRegistView))
        presentViewController(navi, animated: true) {
            
        }
    }
    
     func backToRegistView() {
        contro.dismissViewControllerAnimated(true) { 
            
        }
    }

重点是用上topItem进行设置

外部设置UINavigation 的 Title 和 barbuttonItem_第1张图片
苹果的解释

你可能感兴趣的:(外部设置UINavigation 的 Title 和 barbuttonItem)