How to Create a Custom Pop Up View With Swift

How to create a custom pop up view with swift?

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

Show custom popup view on top of view Hierarchy ~ SwiftUI

At the bottom of the VStack, add the modifier .navigationBarBackButtonHidden().

        ...

} //: Main VSTACK
.padding(.horizontal)
.navigationBarBackButtonHidden(isShowPopup) // <- Here
.navigationBarTitleDisplayMode(.inline)
.toolbar {

...

How to dismiss and present a custom popup (UIViewController) using protocols - SWIFT

You need to set the delegate here

func popUpDimissed() {
let secondPopUpVC = storyboard?.instantiateViewController(withIdentifier: "secondPopUp") as! SecondPopUp
secondPopUpVC.createHTagDelegate = self
present(secondPopUpVC, animated: true, completion: nil)
}

BTW this code inside viewDidLoad

let secondPopUpVC = storyboard?.instantiateViewController(withIdentifier: "createTag") as! SecondPopUp
secondPopUpVC.createHTagDelegate = self

is useless

Edit:

You have to do the same inside

func popUpCreateHTagDismissed() {
let firstPopUpVC = storyboard?.instantiateViewController(withIdentifier: "firstPopUp") as! FirstPopUp
firstPopUpVC.popUpDismissedDelegate = self
present(firstPopUpVC, animated: true, completion: nil)
}

Pop Up View in Swift

This happens, because you have to present your popover as a modal ViewController. To achieve this you have to set the modal presentation style before presenting your popover from your target ViewController. This code should be called in your presenting ViewController:

let vc = YourPopOverViewController(nib: UINib(name: "PopOverViewController", bundle: nil), bundle: nil)
vc.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext

tabBarController.present(vc, animated: true)

EDIT:

This should do the trick if you have designed your PopOverViewController as a fullscreen ViewController. I have done this a bunch of times and have left the space which should not be presented as clear background:

@IBAction func PopUpClicked(_ sender: UIButton) -> Void {
let popOverVC = UIStoryboard(name: "SpinningWheel", bundle: nil).instantiateViewController(withIdentifier: "PhotoPopUp") as! PopUpViewController
popOverVc.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
tabBarController.present(popOverVC, animated: true)
}

Adding Custom popup doesn't filling the full view iOS, Swift

Have you tried using the present() function? Something like this:

let popupVC = PopupViewController()
popupVC.modalPresentationStyle = .overFullScreen
popupVC.modalTransitionStyle = .crossDissolve
self.present(popupVC, animated: true, completion: nil)

I find designing view controllers in their own xib files easier, and it allows initialisation like in my example, rather than having to use the storyboard in code, but thats personal preference.
If it still isnt working, maybe check that the layout constraints for the popup's background are correct, so it actually fills it's parent.



Related Topics



Leave a reply



Submit