TitleView of UITableViewController's UINavigationItem image not visible

87 views Asked by At

I am using Swift 2.3 in Xcode 7.3. I have a UINavigationController with a UITableViewController. I am trying to set the titleView of the navigation item in the navigationBar to create a button centered in the navigation bar.

The indexTextBut is a UIButton hooked up from the story board. I have tried just creating a new button and it made no perceivable difference. The way I am currently doing it the button is there as it has the behavior it should but no image. I have tried just setting the title field as a string and even then I see nothing. I set the right and left barButtonItems using:

navItem.setRightBarButtonItems(barButtonItems, animated: false)
navItemArray.append(navItemCatDet)
self.navigationController?.navigationBar.setItems(navItemArray, animated: false)

Here is my relevant code. My big question is why can't I see the image of the button being set to the titleView. It has preset behavior and when I click where it should be I get that behavior. I've tried adjusting the frame of the UIButton and its UIImageView, and the size of the UIImageView's image but have had no luck. I included the line aobut tinting the background just incase it is related even though I have tried it with and without.

class MyTableViewController: UITableViewController, UITextFieldDelegate {

  @IBOutlet weak var indexTextBut: UIButton!

  override func viewDidLoad() {
    super.viewDidLoad()

    var indexNavItemArray = [UINavigationItem]()
    var indexNavItem = UINavigationItem()

    indexNavItem.titleView = newButton
    indexNavItemArray.append(indexNavItem)

    self.navigationController?.navigationBar.barTintColor = UIColor(netHex:0xe63246)
    self.navigationController?.navigationBar.setItems(indexNavItemArray, animated: false)
1

There are 1 answers

1
john afordis On

swift 3.0

var indexNavItemArray = [UINavigationItem]()
        var indexNavItem = UINavigationItem()

        indexNavItem.titleView = indexTextBut
        indexTextBut.setTitle("button", for: .normal)
        indexNavItemArray.append(indexNavItem)
         self.navigationController?.navigationBar.barTintColor = UIColor.lightGray

        self.navigationController?.navigationBar.setItems(indexNavItemArray, animated: false)

replace your code to above.