Last active
July 9, 2016 22:35
-
-
Save lifeisfoo/fe3bc1cc94a137b697aa to your computer and use it in GitHub Desktop.
iOS CNPPopupController Swift example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Swift CNPPopupController ViewController Example | |
// See https://github.com/carsonperrotti/CNPPopupController | |
// | |
// Created by Alessandro Miliucci on 7 Jan 2016. | |
// [email protected] | |
// | |
import UIKit | |
import CNPPopupController | |
class MyViewController: CNPPopupControllerDelegate { | |
var popupController:CNPPopupController = CNPPopupController() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
func showPopupWithStyle(popupStyle: CNPPopupStyle) { | |
let paragraphStyle = NSMutableParagraphStyle() | |
paragraphStyle.lineBreakMode = NSLineBreakMode.ByWordWrapping | |
paragraphStyle.alignment = NSTextAlignment.Center | |
let title = NSAttributedString(string: "It's A Popup!", attributes: [NSFontAttributeName: UIFont.systemFontOfSize(24), NSParagraphStyleAttributeName: paragraphStyle]) | |
let lineOne = NSAttributedString(string: "You can add text and images", attributes: [NSFontAttributeName: UIFont.systemFontOfSize(18), NSParagraphStyleAttributeName: paragraphStyle]) | |
let lineTwo = NSAttributedString(string: "With style, using NSAttributedString", attributes: [NSFontAttributeName: UIFont.systemFontOfSize(18), NSForegroundColorAttributeName: UIColor.init(colorLiteralRed: 0.46, green: 0.8, blue: 1.0, alpha: 1.0), NSParagraphStyleAttributeName: paragraphStyle]) | |
let button = CNPPopupButton.init(frame: CGRectMake(0, 0, 200, 60)) | |
button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal) | |
button.titleLabel?.font = UIFont.boldSystemFontOfSize(18) | |
button.setTitle("Close Me", forState: UIControlState.Normal) | |
button.backgroundColor = UIColor.init(colorLiteralRed: 0.46, green: 0.8, blue: 1.0, alpha: 1.0) | |
button.layer.cornerRadius = 4; | |
button.selectionHandler = { (CNPPopupButton button) -> Void in | |
self.popupController.dismissPopupControllerAnimated(true) | |
print("Block for button: \(button.titleLabel?.text)") | |
} | |
let titleLabel = UILabel() | |
titleLabel.numberOfLines = 0; | |
titleLabel.attributedText = title | |
let lineOneLabel = UILabel() | |
lineOneLabel.numberOfLines = 0; | |
lineOneLabel.attributedText = lineOne; | |
let imageView = UIImageView.init(image: UIImage.init(named: "icon")) | |
let lineTwoLabel = UILabel() | |
lineTwoLabel.numberOfLines = 0; | |
lineTwoLabel.attributedText = lineTwo; | |
let customView = UIView.init(frame: CGRectMake(0, 0, 250, 55)) | |
customView.backgroundColor = UIColor.lightGrayColor() | |
let textField = UITextField.init(frame: CGRectMake(10, 10, 230, 35)) | |
textField.borderStyle = UITextBorderStyle.RoundedRect | |
textField.placeholder = "Custom view!" | |
customView.addSubview(textField) | |
self.popupController = CNPPopupController(contents:[titleLabel, lineOneLabel, imageView, lineTwoLabel, customView, button]) | |
self.popupController.theme = CNPPopupTheme.defaultTheme() | |
self.popupController.theme.popupStyle = CNPPopupStyle.Centered | |
self.popupController.delegate = self | |
self.popupController.presentPopupControllerAnimated(true) | |
} | |
// MARK: - CNPPopupController Delegate | |
func popupController(controller: CNPPopupController, dismissWithButtonTitle title: NSString) { | |
print("Dismissed with button title \(title)") | |
} | |
func popupControllerDidPresent(controller: CNPPopupController) { | |
print("Popup controller presented") | |
} | |
// Example action - TODO: replace with yours | |
@IBAction func showPopupCentered(sender: AnyObject) { | |
self.showPopupWithStyle(CNPPopupStyle.Centered) | |
} | |
@IBAction func showPopupFormSheet(sender: AnyObject) { | |
self.showPopupWithStyle(CNPPopupStyle.ActionSheet) | |
} | |
@IBAction func showPopupFullscreen(sender: AnyObject) { | |
self.showPopupWithStyle(CNPPopupStyle.Fullscreen) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment