После добавления CustomView в navigationItem, CustomView всегда возвращает ноль

Сильфонный код успешно добавляет customView к navigationItem , но при попытке доступа к customView всегда возвращается nil

override func viewDidLoad() {
    super.viewDidLoad()
    let customView = getCustomView() // supposed that the function return a custom view
    let actionButton = UIBarButtonItem(customView: customView)
    self.navigationItem.rightBarButtonItem = actionButton // successfully added customView
    print(navigationItem.rightBarButtonItem?.customView)  // print always nil
}

Результат:

nil

Всего 1 ответ


let customView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
    customView.backgroundColor = UIColor.blue// supposed that the function return a custom view
    let actionButton = UIBarButtonItem(customView: customView)
    self.navigationItem.rightBarButtonItem = actionButton // successfully added customView
    print(navigationItem.rightBarButtonItem?.customView)

Это работает для меня