将一个按钮置于navigationBar中,可以使用以下方法:
let button = UIButton(type: .system)
button.setTitle("按钮", for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
let barButton = UIBarButtonItem(customView: button)
navigationItem.rightBarButtonItem = barButton
let button = UIButton(type: .system)
button.setTitle("按钮", for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
let barButton = UIBarButtonItem(customView: button)
navigationItem.rightBarButtonItem = barButton
let button = UIButton(type: .system)
button.setTitle("按钮", for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
let barButton = UIBarButtonItem(customView: button)
navigationItem.rightBarButtonItem = barButton
let button = UIButton(type: .system)
button.setTitle("按钮", for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
let barButton = UIBarButtonItem(customView: button)
navigationItem.rightBarButtonItem = barButton
let button = UIButton(type: .system)
button.setTitle("按钮", for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
let barButton = UIBarButtonItem(customView: button)
navigationItem.rightBarButtonItem = barButton
let button = UIButton(type: .system)
button.setTitle("按钮", for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
let barButton = UIBarButtonItem(customView: button)
navigationItem.rightBarButtonItem = barButton
let button = UIButton(type: .system)
button.setTitle("按钮", for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
let barButton = UIBarButtonItem(customView: button)
navigationItem.rightBarButtonItem = barButton
let button = UIButton(type: .system)
button.setTitle("按钮", for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
let barButton = UIBarButtonItem(customView: button)
navigationItem.rightBarButtonItem = barButton
let button = UIButton(type: .system)
button.setTitle("按钮", for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
let barButton = UIBarButtonItem(customView: button)
navigationItem.rightBarButtonItem = barButton
let button = UIButton(type: .system)
button.setTitle("按钮", for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
let barButton = UIBarButtonItem(customView: button)
navigationItem.rightBarButtonItem = barButton
let button = UIButton(type: .system)
button.setTitle("按钮", for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
let barButton = UIBarButtonItem(customView: button)
navigationItem.rightBarButtonItem = barButton
let button = UIButton(type: .system)
button.setTitle("按钮", for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
let barButton = UIBarButtonItem(customView: button)
navigationItem.rightBarButtonItem = barButton
let button = UIButton(type: .system)
button.setTitle("按钮", for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
let barButton = UIBarButtonItem(customView: button)
navigationItem.rightBarButtonItem = barButton
let button = UIButton(type: .system)
button.setTitle("按钮", for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
let barButton = UIBarButtonItem(customView: button)
navigationItem.rightBarButtonItem = barButton
let button = UIButton(type: .system)
button.setTitle("按钮", for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
let barButton = UIBarButtonItem(customView: button)
navigationItem.rightBarButtonItem = barButton
let button = UIButton(type: .system)
button.setTitle("按钮", for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
let barButton = UIBarButtonItem(customView: button)
navigationItem.rightBarButtonItem = barButton
let button = UIButton(type: .system)
button.setTitle("按钮", for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
let barButton = UIBarButtonItem(customView: button)
navigationItem.rightBarButtonItem = barButton
let button = UIButton(type: .system)
button.setTitle("按钮", for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
let barButton = UIBarButtonItem(customView: button)
navigationItem.rightBarButtonItem = barButton
let button = UIButton(type: .system)
button.setTitle("按钮", for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
let barButton = UIBarButtonItem(customView: button)
navigationItem.rightBarButtonItem = barButton
let button = UIButton(type: .system)
button.setTitle("按钮", for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
let barButton = UIBarButtonItem(customView: button)
navigationItem.rightBarButtonItem = barButton
let button = UIButton(type: .system)
button.setTitle("按钮", for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
let barButton = UIBarButtonItem(customView: button)
navigationItem.rightBarButtonItem = barButton
let button = UIButton(type: .system)
button.setTitle("按钮", for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
let barButton = UIBarButtonItem(customView: button)
navigationItem.rightBarButtonItem = barButton
let button = UIButton(type: .system)
button.setTitle("按钮", for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
let barButton = UIBarButtonItem(customView: button)
navigationItem.rightBarButtonItem = barButton
let button = UIButton(type: .system)
button.setTitle("按钮", for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
let barButton = UIBarButtonItem(customView: button)
navigationItem.rightBarButtonItem = barButton
let button = UIButton(type: .system)
button.setTitle("按钮", for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
let barButton = UIBarButtonItem(customView: button)
navigationItem.rightBarButtonItem = barButton
let button = UIButton(type: .system)
button.setTitle("按钮", for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
let barButton = UIBarButtonItem(customView: button)
navigationItem.rightBarButtonItem = barButton
领取专属 10元无门槛券
手把手带您无忧上云