导航项的 titleView 移到了左边?
titleView of Navigation item moved to the left?
我有一个带有左(汉堡包)navigationItem.leftBarButtonItem
的应用程序,我们已将 titleView
设置为公司的徽标。问题是他们想要汉堡包旁边的徽标。汉堡包按钮设置在 Storyboard 的 ViewController
中,徽标以编程方式设置,如下所示:
let logo = UIImage(named: "logo.png")
let imageView = UIImageView(image:logo)
imageView.frame = CGRect(x: 0, y: 0, width: 120, height: 60)
navigationItem.titleView = imageView
有办法把它移到左边吗?
更新:
关于使用leftBarButtonItems
的建议
我在另一个 UIViewController
中完成了此操作,但结果与我预期的不同。
这是代码:
let logo = UIImage(named: "logo")!
let karambaButton = UIBarButtonItem(image: logo, style: .plain, target: self, action: nil)
let backBTN = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: navigationController, action: #selector(UINavigationController.popViewController(animated:)))
navigationItem.leftBarButtonItems = [backBTN, logoButton]
结果如下:
(深色块是图片,因为是客户端所以不得不遮住)
UINavigationItem 有一个名为 leftBarButtonItems
的 属性。
将您的徽标 imageView 放入 UIBarItem 和 size it correctly 的 customView 属性 中,您应该已准备就绪。只需将它与汉堡包按钮一起添加到数组中即可。
编辑添加 -- 尝试使用自定义视图,即:
if let logo = UIImage(named: "logo") {
let imageView = UIImageView(image:logo)
let karambaButton = UIBarButtonItem(customView: imageView)
let backBTN = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: navigationController,
action: #selector(UINavigationController.popViewController(animated:)))
navigationItem.leftBarButtonItems = [backBTN, logoButton]
}
您还可以通过编程方式设置自动布局。
imageView.translatesAutoresizingMaskIntoConstraints = false
let horizontalConstraint = imageView.leadingAnchor.constraint(equalTo: hamburgerButton.trailingAnchor, constant: 10)
let verticalConstraint = newView.centerYAnchor.constraint(equalTo: hamburgerButton.centerYAnchor)
let widthConstraint = newView.widthAnchor.constraint(equalToConstant: 120)
let heightConstraint = newView.heightAnchor.constraint(equalToConstant: 120)
imageView.addConstraints([horizontalConstraint, verticalConstraint, widthConstraint, heightConstraint])
您可以通过访问设置多个左栏按钮项目:
navigationItem.leftBarButtonItems = [barItem1, barItem2]
右侧同理。 :)
我有一个带有左(汉堡包)navigationItem.leftBarButtonItem
的应用程序,我们已将 titleView
设置为公司的徽标。问题是他们想要汉堡包旁边的徽标。汉堡包按钮设置在 Storyboard 的 ViewController
中,徽标以编程方式设置,如下所示:
let logo = UIImage(named: "logo.png")
let imageView = UIImageView(image:logo)
imageView.frame = CGRect(x: 0, y: 0, width: 120, height: 60)
navigationItem.titleView = imageView
有办法把它移到左边吗?
更新:
关于使用leftBarButtonItems
我在另一个 UIViewController
中完成了此操作,但结果与我预期的不同。
这是代码:
let logo = UIImage(named: "logo")!
let karambaButton = UIBarButtonItem(image: logo, style: .plain, target: self, action: nil)
let backBTN = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: navigationController, action: #selector(UINavigationController.popViewController(animated:)))
navigationItem.leftBarButtonItems = [backBTN, logoButton]
结果如下:
(深色块是图片,因为是客户端所以不得不遮住)
UINavigationItem 有一个名为 leftBarButtonItems
的 属性。
将您的徽标 imageView 放入 UIBarItem 和 size it correctly 的 customView 属性 中,您应该已准备就绪。只需将它与汉堡包按钮一起添加到数组中即可。
编辑添加 -- 尝试使用自定义视图,即:
if let logo = UIImage(named: "logo") {
let imageView = UIImageView(image:logo)
let karambaButton = UIBarButtonItem(customView: imageView)
let backBTN = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: navigationController,
action: #selector(UINavigationController.popViewController(animated:)))
navigationItem.leftBarButtonItems = [backBTN, logoButton]
}
您还可以通过编程方式设置自动布局。
imageView.translatesAutoresizingMaskIntoConstraints = false
let horizontalConstraint = imageView.leadingAnchor.constraint(equalTo: hamburgerButton.trailingAnchor, constant: 10)
let verticalConstraint = newView.centerYAnchor.constraint(equalTo: hamburgerButton.centerYAnchor)
let widthConstraint = newView.widthAnchor.constraint(equalToConstant: 120)
let heightConstraint = newView.heightAnchor.constraint(equalToConstant: 120)
imageView.addConstraints([horizontalConstraint, verticalConstraint, widthConstraint, heightConstraint])
您可以通过访问设置多个左栏按钮项目:
navigationItem.leftBarButtonItems = [barItem1, barItem2]
右侧同理。 :)