Изменение размера пользовательского UIBarButtonItem в контроллере навигации - PullRequest
0 голосов
/ 19 января 2019

Я пытаюсь добавить пользовательский UIBarButtonItem в навигацию моего приложения. С помощью следующего кода мне удалось добавить один

let rightButton = UIButton(type: .custom)
    rightButton.frame = CGRect(x: 0, y: 0, width: 10, height: 10)
    rightButton.setImage(UIImage(named: "hamburgerMenuIcon"), for: .normal)
    navigationItem.rightBarButtonItem = UIBarButtonItem(customView: rightButton)                  target: nil, action: nil)

но похоже, что rightButton.frame не работает, поскольку не изменяет размеры кнопки вообще.

1 Ответ

0 голосов
/ 19 января 2019

В iOS 11 на навигационной панели отображается Autolayout, поэтому настройка фрейма может не работать. Используйте следующий код

let rightButton = UIButton(type: .custom)
rightButton.frame = CGRect(x: 0.0, y: 0.0, width: 10, height: 10)
rightButton.setImage(UIImage(named:"hamburgerMenuIcon"), for: .normal)

let menuBarItem = UIBarButtonItem(customView: rightButton)
let currWidth = menuBarItem.customView?.widthAnchor.constraint(equalToConstant: 10)
currWidth?.isActive = true
let currHeight = menuBarItem.customView?.heightAnchor.constraint(equalToConstant: 10)
currHeight?.isActive = true
navigationItem.rightBarButtonItem = menuBarItem
...