เกิดข้อผิดพลาดในการเพิ่มปุ่ม UINavigationbar พร้อมรูปภาพ

ฉันกำลังพยายามเพิ่ม UIBarbutton พร้อมรูปภาพบน UINavigationbar ของฉัน อย่างไรก็ตาม ปุ่มจะย้ายไปที่กึ่งกลางแทนที่จะเก็บไว้ทางซ้าย และทำให้ภาพมีขนาดใหญ่ขึ้น พวกคุณช่วยฉันหน่อยได้ไหม?

ไอคอนแฮมเบอร์เกอร์

  override func viewDidLoad() {
        super.viewDidLoad()
        //Delegate TableView
        self.tableViewTopSell.delegate = self
        //SetupNavBarCustom
        self.navigationController?.navigationBar.CustomNavigationBar()
        let logo = UIImage(named: "tag.png")
        let imageView = UIImageView(image:logo)
        self.navigationItem.titleView = imageView
        //Hamburg Menu
        self.navigationItem.leftBarButtonItem = nil
        let button = UIButton(type: .custom)
        button.setImage(UIImage (named: "hamburgIcon"), for: .normal)
        button.frame = CGRect(x: 0.0, y: 0.0, width: 35.0, height: 35.0)
        //button.addTarget(target, action: nil, for: .touchUpInside)
        let barButtonItem = UIBarButtonItem(customView: button)
        self.navigationItem.leftBarButtonItems = [barButtonItem]

person Bruno Vavretchek    schedule 21.06.2018    source แหล่งที่มา
comment
คุณตั้งค่าไม่มีเป็น leftBarButtonItem ของแถบนำทาง จากนั้นคุณตั้งค่าอาร์เรย์ของรายการให้เหมือนกัน นั่นไม่ซ้ำซากเหรอ?   -  person El Tomato    schedule 21.06.2018


คำตอบ (1)


รูปภาพปุ่มแถบด้านซ้ายแบบกำหนดเอง :

    let btnBack = UIButton()
    btnBack.setImage(#imageLiteral(resourceName: "back"), for: .normal)
    btnBack.frame = CGRect.init(x: 0, y: 0, width: 30, height: 30)
    btnBack.addTarget(self, action: #selector(addTapped), for: .touchUpInside)
    btnBack.imageView?.contentMode = .scaleAspectFit
    let leftBack = UIBarButtonItem.init(customView: btnBack)

    navigationItem.leftBarButtonItem = leftBack

ตรวจสอบขนาดของภาพที่คุณพยายามตั้งค่าสำหรับปุ่มแถบด้านซ้าย ฉันใช้ 35 x 35 สำหรับรูปภาพ 2x และขนาด 53 x 53 สำหรับรูปภาพ 3x

person Sahdevsinh Chavda    schedule 21.06.2018