3

我一直在尝试在导航栏中hamburger的右侧放置一个按钮(三个平行线)titleView,但每次我这样做时,我放入的图像都会覆盖整个导航栏并摆脱我拥有的图像titleView.

如果我在情节提要编辑器中选择默认图像,它将毫无问题地出现在导航栏的右侧,但是一旦我在情节提要编辑器中选择汉堡包按钮,我就会遇到与以前相同的问题。我已经尝试了多个不同的图像,并且我已经稍微更改了代码但没有成功。有没有办法调整我正在使用的图像的大小,以便它正确地适合导航栏,或者我的代码有问题?

下面是我的代码viewController.swift

override func viewDidAppear(_ animated: Bool) {
    let nav = self.navigationController?.navigationBar
    let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))

    imageView.contentMode = .scaleAspectFit

    let titleImage = UIImage(named: "logowhitecircle")
    imageView.image = titleImage

    navigationItem.titleView = imageView

    let menuButton = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 40))

    menuButton.contentMode = .scaleAspectFit

    let menuImage = UIImage(named: "hamburgericon")
    menuButton.setImage(menuImage, for: .normal)

    self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: menuButton)
4

4 回答 4

1

您需要在图像资源中设置图像大小,例如3x = 84px, 2x=56px, 1x = 28px ,

在此处输入图像描述 有关更多信息,请参阅苹果文档:https ://developer.apple.com/design/human-interface-guidelines/ios/icons-and-images/custom-icons/

let menuButton = UIBarButtonItem(image: UIImage(named: "logowhitecircle"), style: .plain, target: self, action: #selector(menuButtonTapped(_:)))
self.navigationItem.rightBarButtonItem = menuBu

于 2019-03-14T07:25:19.260 回答
0

试试这个模块FFBadgedBarButtonItem它很容易使用模块,这里是文档链接

下面是我的代码如何实现它!

let image = UIImage(named: "yourImage")

let finalImage = resizeImage(image: image!, newWidth: 30)

navigationItem.rightBarButtonItem = FFBadgedBarButtonItem(image: finalImage, target: self, action: #selector(rightButtonTouched))

这是调用函数

@objc func rightButtonTouched() {
        // what event you need to perfom by clicking on this button
    }

您需要创建桥接头才能使用此 Obj-C 模块。

: D

于 2019-03-14T07:16:53.623 回答
0

尝试这个:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
            super.viewDidLoad()
            //create a new button
            let button: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
            //set image for button
            button.setImage(UIImage(named: "hamburgericon"), forState: UIControlState.Normal)
            //add function for button
            button.addTarget(self, action: "customButtonPressed", forControlEvents: UIControlEvents.TouchUpInside)
            //set frame
            button.frame = CGRectMake(0, 0, 40, 40)

            let barButton = UIBarButtonItem(customView: button)
            //assign button to navigationbar
           self.navigationItem.rightBarButtonItem = barButton
    }

    //This method will call when you press button.
    func customButtonPressed() {

            println("button pressed")
    }
}
于 2019-03-14T07:20:24.550 回答
0

尝试使用以下代码:

        let imageBurger = UIImage(named: "hamburgericon")!
        let btnLeftMenu = UIButton(type: .system)
        btnLeftMenu.bounds = CGRect(x: 10, y: 0, width: imageBurger.size.width, height: imageBurger.size.height)
        btnLeftMenu.contentEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)

        btnLeftMenu.setImage(imageBurger, for: UIControl.State())
        btnLeftMenu.setTitle(title, for: .normal)
        let leftButton = UIBarButtonItem(customView: btnLeftMenu)
        self.navigationItem.leftBarButtonItem = leftButton
于 2019-03-14T05:54:54.570 回答