[iOS] 드롭다운 의심 메뉴 만들기
소스 보기 import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func tappedButton(_ sender: UIButton) {
let frame = sender.frame
presentPopoverView(frame: frame)
}
func presentPopoverView(frame: CGRect){
let contentVC = ContentViewController()
contentVC.modalPresentationStyle = .popover
contentVC.preferredContentSize = CGSize(width: 100, height: 200)
guard let popoverPresentationController = contentVC.popoverPresentationController else { return }
popoverPresentationController.sourceView = view
popoverPresentationController.sourceRect = frame
popoverPresentationController.permittedArrowDirections = .any
popoverPresentationController.delegate = self
present(contentVC, animated: true, completion: nil)
}
}
extension ViewController: UIPopoverPresentationControllerDelegate {
// iPhoneで表示させる場合に必要
func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
return .none
}
}
대상 보기 import UIKit
class ContentViewController: UIViewController {
let cellHeight: CGFloat = 50
let array = ["1", "10", "100", "1000", "10000", "100000"]
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidLayoutSubviews() {
setTableView(view: view)
}
func setTableView(view: UIView){
let posX: CGFloat = 0
let posY: CGFloat = 0
let width: CGFloat = view.frame.width
let height: CGFloat = view.frame.height
let tableView = UITableView(frame: CGRect(x: posX, y: posY, width: width, height: height))
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "tableViewCell")
tableView.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
tableView.bounces = false
tableView.showsVerticalScrollIndicator = false
tableView.delegate = self
tableView.dataSource = self
view.addSubview(tableView)
}
}
extension ContentViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return cellHeight
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(array[indexPath.row])
}
}
extension ContentViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tableViewCell", for: indexPath)
cell.textLabel?.text = array[indexPath.row]
cell.textLabel?.textAlignment = .center
return cell
}
}
후기
제가 이 보도를 참고하도록 허락해 주십시오.
https://qiita.com/orimomo/items/1a44337de974a72b6266
Reference
이 문제에 관하여([iOS] 드롭다운 의심 메뉴 만들기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/renchild8/items/53a985aa496461b909ed
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func tappedButton(_ sender: UIButton) {
let frame = sender.frame
presentPopoverView(frame: frame)
}
func presentPopoverView(frame: CGRect){
let contentVC = ContentViewController()
contentVC.modalPresentationStyle = .popover
contentVC.preferredContentSize = CGSize(width: 100, height: 200)
guard let popoverPresentationController = contentVC.popoverPresentationController else { return }
popoverPresentationController.sourceView = view
popoverPresentationController.sourceRect = frame
popoverPresentationController.permittedArrowDirections = .any
popoverPresentationController.delegate = self
present(contentVC, animated: true, completion: nil)
}
}
extension ViewController: UIPopoverPresentationControllerDelegate {
// iPhoneで表示させる場合に必要
func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
return .none
}
}
import UIKit
class ContentViewController: UIViewController {
let cellHeight: CGFloat = 50
let array = ["1", "10", "100", "1000", "10000", "100000"]
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidLayoutSubviews() {
setTableView(view: view)
}
func setTableView(view: UIView){
let posX: CGFloat = 0
let posY: CGFloat = 0
let width: CGFloat = view.frame.width
let height: CGFloat = view.frame.height
let tableView = UITableView(frame: CGRect(x: posX, y: posY, width: width, height: height))
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "tableViewCell")
tableView.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
tableView.bounces = false
tableView.showsVerticalScrollIndicator = false
tableView.delegate = self
tableView.dataSource = self
view.addSubview(tableView)
}
}
extension ContentViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return cellHeight
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(array[indexPath.row])
}
}
extension ContentViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tableViewCell", for: indexPath)
cell.textLabel?.text = array[indexPath.row]
cell.textLabel?.textAlignment = .center
return cell
}
}
후기
제가 이 보도를 참고하도록 허락해 주십시오.
https://qiita.com/orimomo/items/1a44337de974a72b6266
Reference
이 문제에 관하여([iOS] 드롭다운 의심 메뉴 만들기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/renchild8/items/53a985aa496461b909ed
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
Reference
이 문제에 관하여([iOS] 드롭다운 의심 메뉴 만들기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/renchild8/items/53a985aa496461b909ed텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)