导航标题背景图像不起作用

时间:2017-03-24 11:47:31

标签: ios swift swift3 uinavigationbar

This my simulator screen shot     导入UIKit

class NewsTableViewController: UITableViewController {

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


}

//这是我的自定义导航栏功能

    func addCustomNavigationBarToView(){

//这里我创建了一个navigationBar变量

        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)
    }
}

1 个答案:

答案 0 :(得分:0)

在addCustomNavigationBarToView函数中,您编写了错误的代码:

您的TableViewController没有任何NavigationBar,但您当前正在将背景图像设置为self.navigationController.navigationBar:

<?php
$errors = array();
if (!empty($_POST["lastName"])) {
    $lastName = trim($_POST["lastName"]);
    if (strlen($lastName) < 2 || 50 < strlen($lastName)) {
        $errors['lastname'] = "The name is not valid";
    }
}
else {
    $errors['lastname'] = "The name is missing";
}

if (!empty($_POST["firstName"])) {
    $firstName = trim($_POST["firstName"]);
    if (strlen($firstName) < 2 || 50 < strlen($firstName)) {
        $errors['firstname'] = "The firstname is not valid";
    }
}
else {
    $errors['firstname'] = "the firstname is missing";
}

if ($errors != "") {
    $_SESSION["errorMsg"]["status"] = "There is A problem";
    $_SESSION["errorMsg"]["details"] = $errors;
    header("Location: test2.php");
    exit();
}
else {
    $_SESSION["errorMsg"]["status"] = "There is NO problem";
    header("Location: test2.php");
    exit();
}
?>

您需要将代码更改为:

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