การสร้างปุ่มย้อนกลับแบบกำหนดเองในแอปของฉัน

ฉันกำลังพยายามเปลี่ยนรูปลักษณ์เริ่มต้นของปุ่มย้อนกลับของตัวควบคุมการนำทางในแอปของฉัน

ฉันต้องการใช้และรูปภาพ (ไอคอน) และลบข้อความ "ย้อนกลับ" โค้ดนี้ขยายรูปภาพข้ามปุ่มและไม่ได้ลบชื่อปุ่ม

let backImg: UIImage = UIImage(named: "icon_back")!
UIBarButtonItem.appearance().setBackButtonBackgroundImage(backImg, for: .normal, barMetrics: .default)

คำแนะนำว่าต้องทำอย่างไรภายใน AppDelegate (didFinishLaunchingWithOptions)


person Sanae Ariuop    schedule 08.08.2017    source แหล่งที่มา
comment
สร้างคลาสย่อยพื้นฐานของ UIViewController ด้วย   -  person El Tomato    schedule 08.08.2017
comment
@ElTomato คุณช่วยอธิบายเพิ่มเติมอีกหน่อยได้ไหม? ขอขอบคุณสำหรับความช่วยเหลือของคุณ.   -  person Sanae Ariuop    schedule 08.08.2017


คำตอบ (2)


วิธีง่ายๆ ในการทำคือเพียงสร้าง BaseViewController หนึ่งรายการสำหรับโปรเจ็กต์ของคุณซึ่งได้มาจาก UIViewController คุณสามารถใช้วิธีทั่วไปใน BaseViewController เพื่อสร้าง custom leftBarButton ของคุณในทุก viewController viewController ที่เหลืออยู่ในโครงการของคุณควรเป็น derived from this BaseViewController

class BaseViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        createLeftBarButton(image: #Pass image here#, width: #Pass width of your image view#) // Create custom back bar button.
    }

    /**Create cutom back bar button*/
    func createLeftBarButton(image: UIImage?, width: CGFloat) {

        let backButton: UIButton = UIButton(frame: CGRect(x: 0, y: 0, width: width, height: 50))
        backButton.imageView?.contentMode = .scaleAspectFill
        backButton.imageView?.bounds = CGRect(x: 0, y: 0, width: width, height: width)
        backButton.setImage(image, for: .normal)
        backButton.setImage(image, for: .highlighted)
        backButton.addTarget(self, action: #selector(leftBarButtonItemPressed(_:)), for: .touchUpInside)

        let leftItem: UIBarButtonItem = UIBarButtonItem(customView: backButton)
        navigationItem.leftBarButtonItem = leftItem

    }

    /**Custom back bar button pressed. So handle here*/
    func leftBarButtonItemPressed(_ sender: UIButton) {

        view.endEditing(true) // End editing if any.
        if isViewControllerPresented() { // Check view controller is presented or pushed
            dismiss(animated: true, completion: nil) // Dismiss ViewController if presented
        } else {
            _ = navigationController?.popViewController(animated: true) // Pop ViewController if pushed
        }

    }

    /**To check whether view controller is presented or pushed.*/
    func isViewControllerPresented() -> Bool {

        if self.presentingViewController?.presentedViewController == self {
            return true
        }
        if (self.navigationController != nil && self.navigationController?.presentingViewController?.presentedViewController == self.navigationController) && self.navigationController?.viewControllers.count == 1 {
            return true
        }
        if self.tabBarController?.presentingViewController is UITabBarController {
            return true
        }
        return false

    }

  }

// Sub class your remaining viewControllers like this.
class FirstViewController: BaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad() // When calling this super method, the custom back bar button will be created for you
    }

}

ขอบคุณ.

person Karthick Selvaraj    schedule 08.08.2017

คุณสามารถทำได้โดยการตั้งค่า navigationItem's BackButtonBackgroundImage;

นอกจากนี้คุณยังสามารถตั้งค่าลักษณะที่ปรากฏสำหรับเอฟเฟกต์ระดับโลก:

let item = UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]) item.setBackButtonBackgroundVerticalPositionAdjustment(-10, for: .default) item.setBackButtonBackgroundImage(youImage, for: .normal, barMetrics: .default)


หากต้องการลบหัวเรื่องด้านหลัง คุณสามารถทำได้ดังนี้:

@implementation UINavigationItem (BackItem)

-(UIBarButtonItem *)backBarButtonItem { UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStylePlain target:nil action:nil]; item.tintColor = [UIColor darkGrayColor]; return item; }

@end

person BruceLiu    schedule 08.08.2017