[iOS][Storyboard]超簡単!ポップアップビューの実装方法
8737 ワード
StoryBoardでポップアップビューを実装する方法をメモ。
1. StoryboardでViewを作成
2. ポップアップビューのViewControllerを実装
import UIKit
class PopupViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// 閉じるボタンがタップされた時
@IBAction func onTapCancel(_ sender: UIButton) {
self.dismiss(animated: false, completion: nil)
}
// ポップアップの外側をタップした時にポップアップを閉じる
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
var tapLocation: CGPoint = CGPoint()
// タッチイベントを取得する
let touch = touches.first
// タップした座標を取得する
tapLocation = touch!.location(in: self.view)
let popUpView: UIView = self.view.viewWithTag(100)! as UIView
if !popUpView.frame.contains(tapLocation) {
self.dismiss(animated: false, completion: nil)
}
}
}
3. 「ポップアップを表示」ボタンをタップした時の表示処理を実装
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// ポップアップを表示がタップされた時
@IBAction func onTapShowPopup(_ sender: UIButton) {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let popupView: PopupViewController = storyBoard.instantiateViewController(withIdentifier: "popupView") as! PopupViewController
popupView.modalPresentationStyle = .overFullScreen
popupView.modalTransitionStyle = .crossDissolve
self.present(popupView, animated: false, completion: nil)
}
}
完成
import UIKit
class PopupViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// 閉じるボタンがタップされた時
@IBAction func onTapCancel(_ sender: UIButton) {
self.dismiss(animated: false, completion: nil)
}
// ポップアップの外側をタップした時にポップアップを閉じる
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
var tapLocation: CGPoint = CGPoint()
// タッチイベントを取得する
let touch = touches.first
// タップした座標を取得する
tapLocation = touch!.location(in: self.view)
let popUpView: UIView = self.view.viewWithTag(100)! as UIView
if !popUpView.frame.contains(tapLocation) {
self.dismiss(animated: false, completion: nil)
}
}
}
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// ポップアップを表示がタップされた時
@IBAction func onTapShowPopup(_ sender: UIButton) {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let popupView: PopupViewController = storyBoard.instantiateViewController(withIdentifier: "popupView") as! PopupViewController
popupView.modalPresentationStyle = .overFullScreen
popupView.modalTransitionStyle = .crossDissolve
self.present(popupView, animated: false, completion: nil)
}
}
完成
Author And Source
この問題について([iOS][Storyboard]超簡単!ポップアップビューの実装方法), 我々は、より多くの情報をここで見つけました https://qiita.com/nakamurau1@github/items/f8f2e106ef0f1967893d著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .