iOS實現(xiàn)Pad上菜單彈出界面
前言:
此種方式實現(xiàn)只適用于pad開發(fā),在iPhone上是無效的。
實現(xiàn):
比如我在界面上有一個按鈕,點擊按鈕,在按鈕旁邊彈出一個Pop框。
1、按鈕點擊事件
btn.addTarget(self, action: #selector(self.popShow), for: .touchUpInside)
2、事件處理
/// 彈框選擇條件
///
/// - Parameter sender: <#sender description#>
func popShow(sender:UIButton) {
let popVC = ExerciseLibPopViewController()
popVC.modalPresentationStyle = .popover
popVC.preferredContentSize = CGSize(width: 111, height: 2*44.0)
popVC.popoverPresentationController?.sourceView = sender
popVC.popoverPresentationController?.sourceRect = sender.bounds
popVC.popoverPresentationController?.delegate = self
self.present(popVC, animated: true, completion: nil)
}
其中,popover類有一個代理:
extension xxxViewController : UIPopoverPresentationControllerDelegate {
func popoverPresentationControllerShouldDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) -> Bool {
print("popoverPresentationControllerShouldDismissPopover")
// 處理你要做的
return true
}
}
類似效果如下:

最后小Tips:
1、如果這里需要把上圖的小箭頭背景色改成其他顏色(和里面顏色一致)
加上這句即可:
popVC.popoverPresentationController?.backgroundColor = UIColor.white
2、如果想改變彈出框位置,修改這句即可:
// 將bounds改成CGRect(x,y,w,h)自己構(gòu)造參數(shù) popVC.popoverPresentationController?.sourceRect = sender.bounds
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS App項目中引入SQLite數(shù)據(jù)庫的教程
SQLite是一個極輕的嵌入式數(shù)據(jù)庫,在應(yīng)用程序中捆綁使用可以更方便地幫助操控關(guān)系型數(shù)據(jù),這里我們就來看一下iOS App項目中引入SQLite數(shù)據(jù)庫的教程2016-06-06
IOS用AFN發(fā)送字符串形式的Json數(shù)據(jù)給服務(wù)器實例
本篇文章主要介紹了IOS用AFN發(fā)送字符串形式的Json數(shù)據(jù)給服務(wù)器實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-04-04
兼容iOS 10 升級xcode8出現(xiàn)的問題及一些適配問題的解決方案
這篇文章主要介紹了兼容iOS 10 升級xcode8出現(xiàn)的問題及一些適配問題的解決方案,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09
IOS百度地圖導(dǎo)航開發(fā)功能實現(xiàn)簡述
百度地圖導(dǎo)航非常實用,那么基于代碼是如何實現(xiàn)的呢,下面通過本文給大家介紹IOS百度地圖導(dǎo)航開發(fā)功能實現(xiàn)簡述,需要的朋友可以參考下本文2016-03-03
iOS中使用NSProgress類來創(chuàng)建UI進度條的方法詳解
NSProgress是iOS7以后引入的用于制作進度條的類,能夠監(jiān)聽多個任務(wù),這里就為大家?guī)韎OS中使用NSProgress類來創(chuàng)建UI進度條的方法詳解,需要的朋友可以參考下2016-06-06

