简体   繁体   English

导航栏项目未左对齐

[英]Navigation Bar Items not left aligned

I have a navigationBar where I would like to have a button on the left, a activityIndicator as title and a doneButton on the right.我有一个navigationBar button ,我想在左侧有一个button ,在右侧有一个activityIndicator作为title和一个doneButton This is what I tried:这是我尝试过的:

    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))

The problem is that googleButton and activityIndicator are not on the left/center:问题是googleButtonactivityIndicator不在左侧/中心:

在此处输入图片说明

What am I missing here?我在这里缺少什么?

Update:更新:

I tried setting the leftBarButtonItem to a simple item with title and it works as expexted.. Why is it not working with the image?我尝试将leftBarButtonItem设置为一个带有title的简单项目,它的工作原理是 expexted .. 为什么它不能处理图像?

I think it has to do with the intrinsicContentSize of the googleButton .我认为这与googleButtonintrinsicContentSize内容googleButton Since you haven't explicitly set it, the size of the google logo image is expanding the width of the button.由于您尚未明确设置它,因此 google 徽标图像的大小正在扩大按钮的宽度。 The intrinsicContentSize property of a UIButton has a set height by default, but none for the width: UIButtonintrinsicContentSize属性默认设置了高度,但没有设置宽度:

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)

You can create a UIImage extension to redraw the size of the google logo:您可以创建一个 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)
    }
}

Change the button name to:将按钮名称更改为:

self.navigationItem.leftBarButtonItem = googleBarButton

Of course, the 25 points for the width and the height is arbitrary.当然,宽度和高度的25个点是任意的。 Adjust it to fit your need.调整它以满足您的需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM