2017-03-24 55 views
1

This my simulator screen shot 進口的UIKit沒有工作導航標題背景圖像

class NewsTableViewController: UITableViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     addCustomNavigationBarToView() 


} 

//這是我的自定義導航欄功能

func addCustomNavigationBarToView(){ 

//這裏我創建的導航欄變量

 let navigationBar = UINavigationBar(frame: CGRect(x:0, y:0, width: self.view.frame.size.width, height: 100)) 
     let navBackgroundImage:UIImage! = UIImage(named: "BarBa.jpg")   self.navigationController?.navigationBar.setBackgroundImage(navBackgroundImage,for: .default) 

//我創建了導航項目變量並設置圖像

 let navigationItem = UINavigationItem() 
     let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 220, height: 90)) 
     imageView.contentMode = .scaleAspectFit 
     let image = UIImage(named: "logo.png") 
     imageView.image = image 
     navigationItem.titleView = imageView 

    // Create left and right button for navigation item 
     let leftButton = UIBarButtonItem(title: "Save", style: UIBarButtonItemStyle.plain, target: self, action: Selector(("saveButtonClicked:"))) 
     navigationItem.leftBarButtonItem = leftButton 
     //tapping the menu button work 
// Assign the navigation item to the navigation bar 
     navigationBar.items = [navigationItem] 
     self.view.addSubview(navigationBar) 
    } 
} 

回答

0

在addCustomNavigationBarToView功能你寫錯誤的代碼:

你TableViewController沒有任何導航欄,但目前要設置背景圖片self.navigationController.navigationBar:

{ self.navigationController?.navigationBar.setBackgroundImage(navBackgroundImage,for: .default) 
    } 

你需要將您的代碼更改爲:

{ 
navigationBar.setBackgroundImage(navBackgroundImage,for: .default) 
}