为什么 navigationItem.leftBarButtonItem 没有显示在左侧?

发布于 2025-01-20 14:24:50 字数 1062 浏览 0 评论 0原文

我已经设置了一个NavigationBar,并且正在尝试在左侧添加一个png,作为左BarbuttonItem,但它出现在NavigationBar的中间。

private func configureNavBar() {
    var image = UIImage(named: "netflixLogo") // PNG
    image = image?.withRenderingMode(.alwaysOriginal)
    //self.navigationController?.navigationBar.topItem?.leftBarButtonItem = UIBarButtonItem(image: image, style: .done, target: self, action: nil) This didn't work either.
    
    navigationItem.leftBarButtonItem = UIBarButtonItem(image: image, style: .done, target: self, action: nil)
    
    //These buttons are working, they are on the right side.
    navigationItem.rightBarButtonItems = [
        UIBarButtonItem(image: UIImage(systemName: "person"), style: .done, target: self, action: nil),
        UIBarButtonItem(image: UIImage(systemName: "play.rectangle"), style: .done, target: self, action: nil),
    ]
    
    navigationController?.navigationBar.tintColor = .white
}

这就是我在模拟器上看到的:

“

I have set up a navigationBar and I'm trying to add a PNG to the left as a leftBarButtonItem but it's showing up in the middle of the navigationBar.

private func configureNavBar() {
    var image = UIImage(named: "netflixLogo") // PNG
    image = image?.withRenderingMode(.alwaysOriginal)
    //self.navigationController?.navigationBar.topItem?.leftBarButtonItem = UIBarButtonItem(image: image, style: .done, target: self, action: nil) This didn't work either.
    
    navigationItem.leftBarButtonItem = UIBarButtonItem(image: image, style: .done, target: self, action: nil)
    
    //These buttons are working, they are on the right side.
    navigationItem.rightBarButtonItems = [
        UIBarButtonItem(image: UIImage(systemName: "person"), style: .done, target: self, action: nil),
        UIBarButtonItem(image: UIImage(systemName: "play.rectangle"), style: .done, target: self, action: nil),
    ]
    
    navigationController?.navigationBar.tintColor = .white
}

This is what I see on the simulator:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明月夜 2025-01-27 14:24:50

它在左侧,但图像太宽,太高了,对于NAV栏而言,显示在其边界的中心安装的方面

It's on the left, but the image is just too wide and tall for the nav bar so is displayed aspect fitted in the center of its bounds

太阳男子 2025-01-27 14:24:50

我有同样的问题。

试试这个:

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

I have same issue.

Try this:

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)
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文