So I am working on a simple app, where I use a table view. This table view displays the name of "players". But in order for me to add players in the table view, I want a pop up window to be displayed with a text field where you provide the name.
Now I have been reading about creating a xib or nib file, but I am not sure how to "load" the pop up window.
What's the best approach to this?
Looks like this:
In storyboard
In storyboard create one UIViewController and design it according to your requirement.
Set custom class as anotherViewController and Storyboard_ID as another_view_sid
Create one new Cocoa Touch Class as anotherViewController and subclass of UIVIewController
In viewController
let popvc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "another_view_sid") as! anotherViewController
self.addChildViewController(popvc)
popvc.view.frame = self.view.frame
self.view.addSubview(popvc.view)
popvc.didMove(toParentViewController: self)
In anotherViewController
override func viewDidLoad() {
super.viewDidLoad()
showAnimate()
}
}
#IBAction func Close_popupView(_ sender: Any) {
removeAnimate()
}
func showAnimate()
{
self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
self.view.alpha = 0.0
UIView.animate(withDuration: 0.25, animations: {
self.view.alpha = 1.0
self.view.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
})
}
func removeAnimate()
{
UIView.animate(withDuration: 0.25, animations: {
self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
self.view.alpha = 0.0
}, completion: {(finished : Bool) in
if(finished)
{
self.willMove(toParentViewController: nil)
self.view.removeFromSuperview()
self.removeFromParentViewController()
}
})
}
you'd create an custom UIView with all respected object needed, from your Controller's viewDidLoad() you'll hide it.
customView.hidden = true
Whenever your user wants to perform some action or task, you unhide it and once the user finished then hide it again or remove from the superView.
customView.hidden = false
Below there is some code to help you start
private var customView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
customView.hidden = true
}
private func loadCustomViewIntoController() {
let customViewFrame = CGRect(x: 0, y: 0, witdh: view.frame.width, height: view.frame.height - 200)
customView = UIView(frame: customViewFrame)
view.addSubview(customView)
customView.hidden = false
// any other objects should be tied to this view as superView
// for example adding this okayButton
let okayButtonFrame = CGRect(x: 40, y: 100, width: 50, height: 50)
let okayButton = UIButton(frame: okayButtonFrame )
// here we are adding the button its superView
customView.addSubview(okayButton)
okayButton.addTarget(self, action: #selector(self.didPressButtonFromCustomView:), forControlEvents:.TouchUpInside)
}
func didPressButtonFromCustomView(sender:UIButton) {
// do whatever you want
// make view disappears again, or remove from its superview
}
#IBAction func rateButton(sender:UIBarButtonItem) {
// this barButton is located at the top of your tableview navigation bar
// when it pressed make sure you remove any other activities that were on the screen, for example dismiss a keyboard
loadCustomViewIntoController()
}
Check it out this github project, It's closed to be production ready, it gives you a better way to deal (present and dismiss) with UIViews
If you only want the player's name then use a UIAlertController containing a textfield
This 3rd party EzPopup (https://github.com/huynguyencong/EzPopup) can help you show it up. Just design your popup in storyboard, then init it.
// init YourViewController
let contentVC = ...
// Init popup view controller with content is your content view controller
let popupVC = PopupViewController(contentController: contentVC, popupWidth: 100, popupHeight: 200)
// show it by call present(_ , animated:) method from a current UIViewController
present(popupVC, animated: true)
Related
I'm relatively new to Swift. I have a main view controller, ViewControllerMain, and a popover view controller, PopUpVC, which only has a label. I have a function, infoClicked that displays the popover with another function (showPopover) when the info button is clicked. When I click the button, I want to change the label text of the popover. However, with the current code, the label always displays "Default".
Here is my code:
class ViewControllerMain: UIViewController, UIPopoverPresentationControllerDelegate, GetTimesUsed {
let tipController: PopUpVC = PopUpVC().self
#IBAction func infoClicked(_ sender: UIButton) {
tipController.tipText = "Success"
showPopover()
}
func showPopover() {
let myViewController = storyboard?.instantiateViewController(withIdentifier: "popupController")
myViewController?.preferredContentSize = CGSize(width: 350, height: 200)
myViewController?.modalPresentationStyle = .popover
let popOver = myViewController?.popoverPresentationController
popOver?.delegate = self
UIView.animate(withDuration: 1, animations: {
self.GifView.alpha = 0.7
})
DispatchQueue.main.asyncAfter(deadline: .now() + 0.45) {
UIView.animate(withDuration: 0.5, animations: {
self.present(myViewController!, animated: true, completion: nil)
})
}
popOver?.permittedArrowDirections = .down
popOver?.sourceView = self.view
var passthroughViews: [AnyObject]?
passthroughViews = [infoButton]
myViewController?.popoverPresentationController?.passthroughViews = (NSMutableArray(array: passthroughViews!) as! [UIView])
popOver?.sourceRect = infoButton.frame
}
}
class PopUpVC: UIViewController {
#IBOutlet weak var tip: UILabel!
var tipText: String = "Default"
override func viewDidLoad() {
super.viewDidLoad()
tip.text = tipText
// Do any additional setup after loading the view.
}
}
Thank you for your help.
As mentioned in comments, you seem to be instantiating the popup controller 2 times, so try it like this in your showPopOver code:
let myViewController = storyboard?.instantiateViewController(withIdentifier: "popupController") as! PopUpVC
myViewController.preferredContentSize = CGSize(width: 350, height: 200)
myViewController.modalPresentationStyle = .popover
myViewController.tipText = '' //set to what ever
I would like to be able to add a modal view in front of my table view, that is displayed relative to the view rather than relative to the table view (as shown in the photos)1. Also how do I prevent the user interacting with the table behind. I have included the code used to animate my view in also. Thanks in advance!
func newPersonAnimateIn() {
self.overlayView.transform = CGAffineTransform.identity
self.view.addSubview(overlayView)
overlayView.center = self.view.center
overlayView.transform = CGAffineTransform.init(scaleX: 1.3, y: 1.3)
overlayView.alpha = 0
UIView.animate(withDuration: 0.4) {
//self.visualEffectView.effect = self.effectHolder
self.overlayView.alpha = 1
self.overlayView.transform = CGAffineTransform.identity
}
personName.becomeFirstResponder()
}
func newPersonAnimateOut() {
self.view.endEditing(true)
UIView.animate(withDuration: 0.3, animations: {
self.overlayView.alpha = 0
self.overlayView.transform = CGAffineTransform.identity
}){(success:Bool) in
self.overlayView.removeFromSuperview()
}
personName.text = ""
}
#IBAction func addPerson(_ sender: UIBarButtonItem) {
newPersonAnimateIn()
}
#IBAction func continueButton(_ sender: Any) {
newPersonAnimateOut()
}
Maybe this would be enough:
self.tableView.isUserInteractionEnabled = false
or create another view under your modal view that covers full screen and set isUserInteractionEnabled = false
Alright, hope I can explain this as clearly as possible.
I have a Parent ViewController which has a ContainerView, this will contain different viewControllers/Class. I'm changing the content of this container at runtime like this:
let newController = (storyboard?.instantiateViewController(withIdentifier: classIdentifierFromStoryboard))! as UIViewController
let oldController = childViewControllers.last! as UIViewController
oldController.willMove(toParentViewController: nil)
addChildViewController(newController)
newController.view.frame = oldController.view.frame
transition(from: oldController, to: newController, duration: 0, options: .transitionCrossDissolve, animations:{ () -> Void in
}, completion: { (finished) -> Void in
})
oldController.removeFromParentViewController()
newController.didMove(toParentViewController: self)
So for example, the current controller that shows on UI is firstVC, which kinda look like
class firstVC {
func removeAnnotations() {
//something that removes annotation from MapVC
}
}
On a certain event on the parent controller, how can I access the instance of firstVC so that i'll be able to call removeAnnotations? As of the moment I am getting nil, using this on parent controller firstVC.removeAnnotations, assumably this calls another instance of the class. Any help would be great! Hope my explanation makes sense. Thanks!
You could run down the child view controllers like this:
for case let vc as firstVC in self.childViewControllers {
vc.removeAnnotations()
}
This will run down all the child view controllers that are of type firstVC and execute the removeAnnotations method
in this code is child view controller ProfileViewController
in first create one object of child view controller like this
var objectProfile:ProfileViewController!
then viewDidLoad method
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
objectProfile = storyboard.instantiateViewController(withIdentifier: "ProfileViewController") as! ProfileViewController
objectProfile.view.frame = CGRect(x: -(self.view.frame.size.width - 40), y: 0, width: self.view.frame.size.width - 40, height: self.view.frame.size.height)
self.view.addSubview(objectProfile.view)
self.navigationController?.didMove(toParentViewController: objectProfile)
objectProfile.view.isHidden = true
}
then button click method code here
#IBAction func btnProfilePressed(_ sender: UIButton) {
UIView.animate(withDuration: 0.3) {
self.objectProfile.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width - 40, height: self.view.frame.size.height)
}
}
then touchesBegan
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
objectProfile.view.isHidden = false
UIView.animate(withDuration: 0.3) {
self.objectProfile.view.frame = CGRect(x: -(self.view.frame.size.width - 40), y: 0, width: self.view.frame.size.width - 40, height: self.view.frame.size.height)
}
}
complete set this code out put is
I have an app that contains a central view controller with 3 child view controllers in a scrollview that represent sections of the app. General flow of my app goes as follows
LoginVC -> AppVC [Settings, Inbox, Email]
I currently have my logout button in my Settings view controller however the actual logout function needs to be in AppVC. I have tried implementing a delegate but cannot seem to get it working correctly
class Settings: UIViewController {
var delegate: LogoutDelegate?
#IBAction func logout(_ sender: Any) {
delegate?.logoutOfApp()
}
}
protocol LogoutDelegate: class {
func logoutOfApp()
}
----------------------------------
class AppVC: UIViewController, LogoutDelegate {
func logoutOfApp() {
print("This should be called")
//Execute logout code
}
}
Can anyone help me figure out why my delegate function isnt being called in my AppVC?
Update
This is how I set up my child view controllers in a scrollview
func setupViews() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let settingsVC = storyboard.instantiateViewController(withIdentifier: "SettingsVC")
let inboxVC = storyboard.instantiateViewController(withIdentifier: "InboxVC")
let emailVC = storyboard.instantiateViewController(withIdentifier: "EmailVC")
let viewControllers = [settingsVC, inboxVC, emailVC]
let width = scrollView.bounds.width
let height = scrollView.bounds.height
scrollView.contentSize = CGSize(width: width * 3, height: height)
var idx = 0
for vc in viewControllers {
addChildViewController(vc)
let originX = CGFloat(idx) * width
vc.view.frame = CGRect(x: originX, y: 0, width: width, height: height)
scrollView.addSubview(vc.view)
vc.didMove(toParentViewController: self)
idx += 1
}
scrollView.setContentOffset(CGPoint(x: width, y: 0), animated: false)
}
I am not sure where to set my delegate. I have tried to set it inside my for loop self.delegate = vc but always get the same error of not being able to set the delegate
The problem is that you never set the delegate, otherwise the delegate will always be nil and delegate?.logoutOfApp() won't be executed.
In AppVC, you need to do something like:
override func viewDidLoad() {
super.viewDidLoad()
settings.delegate = self
}
I am using a UISplitViewController, with a MasterViewController and DetailViewController, without UINavigationControllers.
Currenly the segue animation for master->detail, triggered by
performSegueWithIdentifier("showDetail", sender: self)
consists in the DetailViewController showing up from the bottom upwards.
How can I make that animation showing left-wards?
I've recently needed to have more control of how the segues are being performed, so I made my custom segue classes which all perform the transition in different directions. Here's one of the implementations:
Swift 2.x
override func perform() {
//credits to http://www.appcoda.com/custom-segue-animations/
let firstClassView = self.sourceViewController.view
let secondClassView = self.destinationViewController.view
let screenWidth = UIScreen.mainScreen().bounds.size.width
let screenHeight = UIScreen.mainScreen().bounds.size.height
secondClassView.frame = CGRectMake(screenWidth, 0, screenWidth, screenHeight)
if let window = UIApplication.sharedApplication().keyWindow {
window.insertSubview(secondClassView, aboveSubview: firstClassView)
UIView.animateWithDuration(0.4, animations: { () -> Void in
firstClassView.frame = CGRectOffset(firstClassView.frame, -screenWidth, 0)
secondClassView.frame = CGRectOffset(secondClassView.frame, -screenWidth, 0)
}) {(Finished) -> Void in
self.sourceViewController.navigationController?.pushViewController(self.destinationViewController, animated: false)
}
}
}
This one will have a "right to left" transition. You can modify this function for your needs by simply changing the initial and ending positions of the source and destination view controller.
Also don't forget that you need to mark your segue as "custom segue", and to assign the new class to it.
UPDATE: Added Swift 3 version
Swift 3
override func perform() {
//credits to http://www.appcoda.com/custom-segue-animations/
let firstClassView = self.source.view
let secondClassView = self.destination.view
let screenWidth = UIScreen.main.bounds.size.width
let screenHeight = UIScreen.main.bounds.size.height
secondClassView?.frame = CGRect(x: screenWidth, y: 0, width: screenWidth, height: screenHeight)
if let window = UIApplication.shared.keyWindow {
window.insertSubview(secondClassView!, aboveSubview: firstClassView!)
UIView.animate(withDuration: 0.4, animations: { () -> Void in
firstClassView?.frame = (firstClassView?.frame.offsetBy(dx: -screenWidth, dy: 0))!
secondClassView?.frame = (secondClassView?.frame.offsetBy(dx: -screenWidth, dy: 0))!
}, completion: {(Finished) -> Void in
self.source.navigationController?.pushViewController(self.destination, animated: false)
})
}
}
Embed your view controllers in UINavigationControllers.
Per the SplitViewController template:
On smaller devices it's going to have to use the Master's navigationController.
Furthermore this question has been answered here and here and here
More from the View Controller Programming Guide:
There are two ways to display a view controller onscreen: embed it in
a container view controller or present it. Container view controllers
provide an app’s primary navigation….
In storyboards it's not that difficult to embed something in a navigation controller. Click on the view controller you want to embed, then Editor->embed in->navigation controller.
--Swift 3.0--
Armin's solution adapted for swift 3.
New -> File -> Cocoa Touch Class -> Class: ... (Subclass: UIStoryboardSegue).
import UIKit
class SlideHorSegue: UIStoryboardSegue {
override func perform() {
//credits to http://www.appcoda.com/custom-segue-animations/
let firstClassView = self.source.view
let secondClassView = self.destination.view
let screenWidth = UIScreen.main.bounds.size.width
let screenHeight = UIScreen.main.bounds.size.height
secondClassView?.frame = CGRect(x: screenWidth, y: 0, width: screenWidth, height: screenHeight)
if let window = UIApplication.shared.keyWindow {
window.insertSubview(secondClassView!, aboveSubview: firstClassView!)
UIView.animate(withDuration: 0.4, animations: { () -> Void in
firstClassView?.frame = (firstClassView?.frame)!.offsetBy(dx: -screenWidth, dy: 0)
secondClassView?.frame = (secondClassView?.frame)!.offsetBy(dx: -screenWidth, dy: 0)
}) {(Finished) -> Void in
self.source.navigationController?.pushViewController(self.destination, animated: false)
}
}
}
}
In storyboard: mark your segue as "custom segue", and to assign the new class to it.
Note: If you have a UIScrollView in your detailesVC, this won't work.
It sounds as though the new view controller is presenting modally. If you embed the detailViewController into a UINavigationController and push the new controller it will animate from right to left and should show a back button too by default.
When you have a compact-width screen,"Show Detail" segue fall back to modal segue automatically.So your DetailViewController will appear vertically as the default modal segue.
You can use a UIViewControllerTransitioningDelegate to custom the animation of a modal segue.
Here is an example to achieve the horizontal animation:
1. set the transitioningDelegate and it's delegate method
class MasterViewController: UITableViewController,UIViewControllerTransitioningDelegate {
//prepare for segue
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
let detailVC = segue.destinationViewController as! DetailViewController
detailVC.transitioningDelegate = self
// detailVC.detailItem = object//Configure detailVC
}
}
//UIViewControllerTransitioningDelegate
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return LeftTransition()
}
func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
let leftTransiton = LeftTransition()
leftTransiton.dismiss = true
return leftTransiton
}
}
2: a custom UIViewControllerAnimatedTransitioning : LeftTransition
import UIKit
class LeftTransition: NSObject ,UIViewControllerAnimatedTransitioning {
var dismiss = false
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
return 2.0
}
func animateTransition(transitionContext: UIViewControllerContextTransitioning){
// Get the two view controllers
let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!
let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!
let containerView = transitionContext.containerView()!
var originRect = containerView.bounds
originRect.origin = CGPointMake(CGRectGetWidth(originRect), 0)
containerView.addSubview(fromVC.view)
containerView.addSubview(toVC.view)
if dismiss{
containerView.bringSubviewToFront(fromVC.view)
UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
fromVC.view.frame = originRect
}, completion: { (_ ) -> Void in
fromVC.view.removeFromSuperview()
transitionContext.completeTransition(true )
})
}else{
toVC.view.frame = originRect
UIView.animateWithDuration(transitionDuration(transitionContext),
animations: { () -> Void in
toVC.view.center = containerView.center
}) { (_) -> Void in
fromVC.view.removeFromSuperview()
transitionContext.completeTransition(true )
}
}
}
}