티스토리 뷰

반응형

UIKit Popover

 

SwiftUI 버전 보러가기

 

import UIKit

// 팝오버
class ViewController: UIViewController {
    
    let button = UIButton(type: .system)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        button.setTitle("Popover", for: .normal)
        
        let action = UIAction(handler: showPopover)
        button.addAction(action, for: .touchUpInside)
        
        view.addSubview(button)
        button.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            button.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            button.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        ])
    }
    
    func showPopover(action: UIAction) {
        let viewController = UIViewController()
        viewController.view.backgroundColor = .systemCyan
        
        viewController.preferredContentSize = .init(width: 200, height: 100)
        viewController.modalPresentationStyle = .popover
        
        let popoverPresentationController = viewController.popoverPresentationController
        popoverPresentationController?.permittedArrowDirections = [.down]
        popoverPresentationController?.sourceRect = button.bounds
        popoverPresentationController?.sourceView = button
        popoverPresentationController?.delegate = self // 필수
        present(viewController, animated: true)
    }
}

extension UIViewController: UIPopoverPresentationControllerDelegate {
    // Popover 스타일 표시하려면 필수
    public func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
        .none
    }
}

#Preview {
    ViewController()
}

팝오버

모달

툴팁

드롭다운

말풍선

채팅

chat

Speech bubble

셀렉트 박스

Select Box

iOS

Swift

UIKit

Xcode

Objective-C

반응형
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
글 보관함