Membuat tombol kembali khusus di aplikasi saya

Saya mencoba mengubah tampilan default tombol kembali Pengontrol Navigasi di aplikasi saya.

Saya ingin menggunakan dan gambar (ikon) dan menghapus teks "Kembali". Kode ini membentangkan gambar melintasi tombol dan tidak menghilangkan judul tombol.

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

Rekomendasi bagaimana melakukannya ada di dalam AppDelegate (didFinishLaunchingWithOptions)?


person Sanae Ariuop    schedule 08.08.2017    source sumber
comment
Buat subkelas dasar UIViewController dengannya.   -  person El Tomato    schedule 08.08.2017
comment
@ElTomato Bisakah Anda menjelaskannya lebih lanjut? Terima kasih untuk bantuannya.   -  person Sanae Ariuop    schedule 08.08.2017


Jawaban (2)


Cara mudahnya adalah dengan membuat satu BaseViewController untuk proyek Anda yang berasal dari UIViewController. Anda dapat menggunakan metode umum di BaseViewController untuk membuat custom leftBarButton di setiap viewController. ViewController yang tersisa di proyek Anda harus 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
    }

}

Terima kasih.

person Karthick Selvaraj    schedule 08.08.2017

Anda dapat melakukan ini dengan mengatur navigationItem's BackButtonBackgroundImage;

Anda juga dapat mengatur tampilan untuk efek global:

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


untuk menghapus judul belakang, Anda dapat melakukan seperti ini:

@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