导航栏项目未左对齐
Navigation Bar Items not left aligned
我有一个 navigationBar
,我想在左边有一个 button
,一个 activityIndicator
作为 title
,一个 doneButton
在左边正确的。这是我试过的:
let activityIndicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 25, height: 25))
let googleImage = UIImage(named: "google")!
let googleButton = UIBarButtonItem(image: googleImage, style: .plain, target: self, action: #selector(googleButtonTapped))
googleButton.image = UIImage(named: "google")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
navigationController?.navigationBar.barTintColor = UIColor.blueCustom
navigationController?.navigationBar.tintColor = UIColor.white
activityIndicator.hidesWhenStopped = true
activityIndicator.color = .white
self.navigationItem.titleView = self.activityIndicator
self.navigationItem.leftBarButtonItem = googleButton
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Fertig", style: .done, target: self, action: #selector(doneTapped))
问题是 googleButton
和 activityIndicator
不在 left/center:
我在这里错过了什么?
更新:
我尝试使用 title
将 leftBarButtonItem
设置为一个简单的项目,它可以正常工作。为什么它不能使用图像?
我认为这与 googleButton
的 intrinsicContentSize
有关。由于您没有明确设置它,google 徽标图像的大小正在扩展按钮的宽度。 UIButton
的intrinsicContentSize
属性默认设置高度,但是none设置宽度:
let googleImage = UIImage(named: "google")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal).resizeTo(size: CGSize(width: 25, height: 25))
let googleButton = UIButton()
googleButton.setBackgroundImage(googleImage, for: .normal)
googleButton.addTarget(self, action: #selector(googleButtonTapped), for: .touchUpInside)
let googleBarButton = UIBarButtonItem(customView: googleButton)
您可以创建一个 UIImage 扩展来重绘 google 徽标的大小:
extension UIImage {
func resizeTo(size: CGSize) -> UIImage {
let renderer = UIGraphicsImageRenderer(size: size)
let image = renderer.image { _ in
self.draw(in: CGRect.init(origin: CGPoint.zero, size: size))
}
return image.withRenderingMode(self.renderingMode)
}
}
将按钮名称更改为:
self.navigationItem.leftBarButtonItem = googleBarButton
当然,宽度和高度的25点是任意的。根据您的需要进行调整。
我有一个 navigationBar
,我想在左边有一个 button
,一个 activityIndicator
作为 title
,一个 doneButton
在左边正确的。这是我试过的:
let activityIndicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 25, height: 25))
let googleImage = UIImage(named: "google")!
let googleButton = UIBarButtonItem(image: googleImage, style: .plain, target: self, action: #selector(googleButtonTapped))
googleButton.image = UIImage(named: "google")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
navigationController?.navigationBar.barTintColor = UIColor.blueCustom
navigationController?.navigationBar.tintColor = UIColor.white
activityIndicator.hidesWhenStopped = true
activityIndicator.color = .white
self.navigationItem.titleView = self.activityIndicator
self.navigationItem.leftBarButtonItem = googleButton
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Fertig", style: .done, target: self, action: #selector(doneTapped))
问题是 googleButton
和 activityIndicator
不在 left/center:
我在这里错过了什么?
更新:
我尝试使用 title
将 leftBarButtonItem
设置为一个简单的项目,它可以正常工作。为什么它不能使用图像?
我认为这与 googleButton
的 intrinsicContentSize
有关。由于您没有明确设置它,google 徽标图像的大小正在扩展按钮的宽度。 UIButton
的intrinsicContentSize
属性默认设置高度,但是none设置宽度:
let googleImage = UIImage(named: "google")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal).resizeTo(size: CGSize(width: 25, height: 25))
let googleButton = UIButton()
googleButton.setBackgroundImage(googleImage, for: .normal)
googleButton.addTarget(self, action: #selector(googleButtonTapped), for: .touchUpInside)
let googleBarButton = UIBarButtonItem(customView: googleButton)
您可以创建一个 UIImage 扩展来重绘 google 徽标的大小:
extension UIImage {
func resizeTo(size: CGSize) -> UIImage {
let renderer = UIGraphicsImageRenderer(size: size)
let image = renderer.image { _ in
self.draw(in: CGRect.init(origin: CGPoint.zero, size: size))
}
return image.withRenderingMode(self.renderingMode)
}
}
将按钮名称更改为:
self.navigationItem.leftBarButtonItem = googleBarButton
当然,宽度和高度的25点是任意的。根据您的需要进行调整。