在iOS开发中,Storyboard是一种直观的方式来设计和布局用户界面。要将UIButton添加为UIBarButtonItem,可以通过以下步骤实现:
如果你更喜欢使用代码来实现,可以按照以下步骤进行:
// 创建一个UIButton
let button = UIButton(type: .system)
button.setTitle("按钮", for: .normal)
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
// 创建一个UIBarButtonItem
let barButton = UIBarButtonItem(customView: button)
// 将UIBarButtonItem添加到导航栏
navigationItem.rightBarButtonItem = barButton
@objc func buttonTapped() {
// 处理按钮点击事件
print("按钮被点击了")
}
isUserInteractionEnabled
属性设置为true
。action
是否正确设置。通过以上步骤和示例代码,你应该能够成功地将UIButton添加为UIBarButtonItem,并在Storyboard中进行布局和自定义。
没有搜到相关的沙龙