2016-12-11 40 views
1

我真的不知道如何調用此功能,音樂應用程序已達到8.4,它看起來像截圖。我想在我的應用程序中實現它,所以當用戶按下單元格時,帶有2個選項按鈕的「泡泡」出現。 我對如何在Obj-C中實現它感興趣,但我相信人們會用Swift編寫答案。由於 ScreenshotiOS - 如何在UITableView的單元格上添加文本/按鈕的氣泡?

+1

這就是所謂的菜單。看看:https://developer.apple.com/reference/uikit/uimenucontroller –

回答

1

我在做同樣的事情,你想要什麼實現UIMenuController。 爲此,您必須創建您自己的視圖控制器,而不是UIMenuItem。截屏如下。

enter image description here

我在做什麼是創建一個的viewController作爲彈出(PopoverMenuController),以及加入的tableView作爲菜單子視圖。 以同樣的方式,你可以添加任何你想要的UI控件而不是tableView。

這裏是你如何使用我的PopoverMenuController。

var myPopupMenu: PopoverMenuController! 

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    // make cell fully visible 
    let tableCell = tableView.cellForRow(at: indexPath)! 
    tableView.scrollRectToVisible(tableView.rectForRow(at: indexPath), animated: false) 
    // pop up menu 
    myPopupMenu = PopoverMenuController() 
    myPopupMenu.sourceView = tableCell 
    var rect = tableCell.bounds 
    // I'm adjusting popup menu position slightly (this is optional) 
    rect.origin.y = rect.size.height/3.0 
    rect.size.height = rect.size.height/2.0 
    myPopupMenu.sourceRect = rect 
    myPopupMenu.addAction(PopMenuAction(textLabel: "MyMenu-1", accessoryType: .none, handler: myMenuHandler1)) 
    myPopupMenu.addAction(PopMenuAction(textLabel: "MyMenu-2", accessoryType: .none, handler: myMenuHandler2)) 
    present(myPopupMenu, animated: true, completion: nil) 
} 

func myMenuHandler1(_ action: PopMenuAction?) -> Bool { 
    // do some work... 
    return false 
} 

func myMenuHandler2(_ action: PopMenuAction?) -> Bool { 
    // do some work... 
    return false 
} 

要轉移到Obj-C不應該​​是一個很大的努力。 我把PopoverMenuController.swift的源代碼here。這個控制器是獨立的,它有一個關於如何使用的描述。

希望這會有所幫助。

相關問題