2017-11-09 17 views
0

長押しのジェスチャー認識機能をUIBarButtonItemに追加したいのですが、できません。ストーリーボードを使用する可能性はなく、方法addGestureRecognizerUIBarButtonItemにありません。Swift UIBarButtonItemジェスチャー認識機能を追加

どうすればこの問題を解決できますか?

+0

考えられる1つの考え方については、https://stackoverflow.com/questions/32517434/ios-swift-how-to-implement-longpressed-action-for-backbuttonを参照してください。 – rmaddy

答えて

0

は、次の方法を試すことができます:

//1. Create A UIButton Which Can Have A Gesture Attached 
    let button = UIButton(type: .custom) 
    button.frame = CGRect(x: 0, y: 0, width: 80, height: 40) 
    button.setTitle("Press Me", for: .normal) 

    //2. Create The Gesture Recognizer 
    let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(doSomething)) 
    longPressGesture.minimumPressDuration = 1 
    button.addGestureRecognizer(longPressGesture) 

    //3. Create A UIBarButton Item & Initialize With The UIButton 
    let barButton = UIBarButtonItem(customView: button) 

    //4. Add It To The Navigation Bar 
    self.navigationItem.leftBarButtonItem = barButton 

はもちろんセレクター方法は、独自の方法で置き換えることになります。

関連する問題