Swift 3 Popover Dim Background - swift

I have read multiple places with suggestions on how to accomplish this. I went with adding a UI view in the background and setting it to disable and then after showing the popover, setting the view to enable.
As you can see it looks to work nicely:
But I do have two problems. The first one is once the popover is presented, you can tap anywhere on the background to dismiss the popover. Is there anywhere to block this from happening? I assumed my background UIView would block any inputs.
Also, after the popover is dismissed, the screen is still dim. I tried the following but neither of them load after dismissing the popover so the View never gets set back to disable:
override func viewDidAppear(_ animated: Bool) {
dimView.isHidden = true
}
override func viewWillAppear(_ animated: Bool) {
dimView.isHidden = true
}
EDIT:
This is the code that I use to present the popover:
let popover = storyboard?.instantiateViewController(withIdentifier: "PopoverVC")
popover?.modalPresentationStyle = .popover
popover?.popoverPresentationController?.delegate = self as? UIPopoverPresentationControllerDelegate
popover?.popoverPresentationController?.sourceView = self.view
popover?.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
popover?.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
dimView.isHidden = false
self.present(popover!, animated: false)

I realize that, dimView is not in PopoverVC, add it into PopoverVC and handle dismiss when tap on it.After the popover is dismissed viewDidAppear and viewWillAppear will not be called. So your screen is still blurry.If you add dimView into Popover, hope you can solve these issuses

I think you could solve your two problems with the UIPopoverPresentationControllerDelegate and a protocol/ delegate to tell the presenting viewcontroller when your are dismissing and hide your dimView.
The first issue can be implemented like this:
extension YourViewController: UIPopoverPresentationControllerDelegate {
func popoverPresentationControllerShouldDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) -> Bool {
return false
}
For the second issue you can pass a function through delegation. Hopefully this link will help with that.
https://matteomanferdini.com/how-ios-view-controllers-communicate-with-each-other/
Cheers

Related

Swift | UIViewController not showing as PopUp but Fullscreen

So i have a viewcontroller which is basically an alert window which is supposed to be a popup and be dismissed by the tap on outside its frame.
But whenever i call that VC, it is always displayed as fullscreen and not as a pop up window.
I have tried a couple of ways to do this, namely as mentioned below.
if let exp : String = expiredVehicles[i] {
expiredVehicleNumber = expiredVehicles[i]
let popUpVC = SubscriptionExpired()
popUpVC.modalTransitionStyle = .crossDissolve
popUpVC.modalPresentationStyle = .popover // also tried other presentation styles but none work and it is still fullscreen
popUpVC.view.backgroundColor = UIColor.white.withAlphaComponent(0.8)
self.present(popUpVC, animated: true, completion: nil)
}
in case anyone need to see the definition of that VC, i will be glad to share it
i feel i should mention that the VC to be displayed as a popup is inheriting UIViewController
Any insight that might help would be great.
Thanks for the input
One potential way is to add a tap gesture recognizer to your View, which dismisses the VC.
But this will only be helpful if this popup has read-only info and doesn't require any further action from the user.
func addTapRecognizer(){
let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap))
self.view.addGestureRecognizer(tap)
}
#objc func handleTap(){
// dismiss the VC here
}
}
You can call following method to show popup:
let popupVC = SubscriptionExpired()
popupVC.modalPresentationStyle = .overCurrentContext
self.addChild(popupVC)
popupVC.view.frame = self.view.frame
self.view.addSubview(popupVC.view)
popupVC.didMove(toParent: self)
}
Then, for removing that popup you can use:
UIView.animate(withDuration: 0.25, animations: {
self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
self.view.alpha = 0.0
}, completion: { (finished) in
if finished {
self.view.removeFromSuperview()
}
})
In that case I have a button inside popup and whenever that button pressed above method triggers. Can you please try it? I can edit my answer according to your needs.
You need to implement this in you presenting view controller:
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
// Return no adaptive presentation style, use default presentation behaviour
return .none
}
UIPopoverPresentationController displaying popover as full screen

small navigation title is showing for few seconds

I have set large title in viewWillAppear()
self.navigationItem.largeTitleDisplayMode = .always
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationItem.title = "Reports"
But still when I redirect to next VC and come back I can see navigation Title in small size for a while and then I see large title, anybody know why and how to fix it?
If you want the next VC to have largeTitleDisplayMode false, you could try to set it in viewWillDisappear() of the current VC, like this:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.navigationBar.prefersLargeTitles = false
}
You should set this in the viewDidLoad of your view controller, not the viewWillAppear. It's the first part in the view lifecycle and where this work should be done

Present below current view and not above

I'm trying to present a view controller below another presented view controller (like WhatsApp when you open camera and press gallery).
I tried many things but none worked ..
Use child view controller and set view of that added child view controller at the top of hierarchy. It will be top most element, so actual background of this view will be obscured, but that's the way to go.
//code inside UIViewController class
func addViewControllerAtBottom() {
let newVC = NewVCType() //just instantiate it
addChildViewController(newVC)
view.insertSubview(newVC.view, at: 0) //at 0 means it's first rendered, all others will be on top of it
}
You can reproduce this behavior by doing the following :
First, create a NavigationController with a root ViewController :
let navController = UINavigationController(rootViewController: firstController)
Then, present this navigationController with animated: false and in the completion of the present method, push your second ViewController, still with animated: false (to avoid weird animations) :
present(navController, animated: false) {
navController.pushViewController(secondController, animated: false)
}
Here you go, you got a new navigation with 2 UIViewController, like WhatsApp.
Full code, wrapped into a button's action :
#IBAction func buttonTapped(_ sender: Any) {
let navController = UINavigationController(rootViewController: firstController)
present(navController, animated: false) {
navController.pushViewController(secondController, animated: false)
}
}

When I go back to a view controller after opening it previously it does not animate labels Xcode Swift

When I open my app I have this label that animates in from outside the screen. When I open the view controller for the first time it works. However, when I go to another view controller and then go back to the initial view controller this label will just be there and not animate in.
levelsLabel.center = CGPoint(x:levelsLabel.center.x - 500, y:levelsLabel.center.y)
UIView.animate(withDuration: 2) {
self.levelsLabel.center = CGPoint(x:self.levelsLabel.center.x + 500, y:self.levelsLabel.center.y)
}
Anyone have any suggestions?? Thank You!
Putting your block of code in viewWillAppear rather than viewDidLoad will make it work. However, according to Apple Doc.
viewDidAppear: Use this method to trigger any operations that need
to occur as soon as the view is presented onscreen, such as fetching
data or showing an animation.
The following is what I'd recommend you to do.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
levelsLabel.center = CGPoint(x:levelsLabel.center.x - 500, y:levelsLabel.center.y)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
UIView.animate(withDuration: 2) {
self.levelsLabel.center = CGPoint(x:self.levelsLabel.center.x + 500, y:self.levelsLabel.center.y)
}
}

SIGABRT when adding a constraint to a uiview

Once again auto layout has me scratching my head. I define a UIView and UITextField at the top of my class:
var urlField = UITextField(frame: .zero)
var barView = UIView(frame: .zero)
I call my configuration method from viewWillAppear (so I know that self.view is all set):
override func viewWillAppear(_ animated: Bool) {
configureURLBar()
}
And I'm doing nothing special there.
func configureURLBar() {
barView.translatesAutoresizingMaskIntoConstraints = false
barView.topAnchor.constraint(equalTo: self.view.layoutMarginsGuide.topAnchor).isActive = true
barView.heightAnchor.constraint(equalToConstant: 30).isActive = true
}
When it gets to the topAnchor constraint I get the exception, and I don't understand why. If I reverse the 2nd and 3rd lines it doesn't blow up until it gets to the .topAnchor line, so that's the problem. I can ask a second question once I understand the reason for this exception (in case you're wondering what I'm trying to do): how do I add a user input (url) bar at the top of my UIWebView. (that didn't work either - same results if I try to constrain against my self.webView which is displaying perfectly) Also: I call self.view.setNeedsDisplay() before viewWillAppear() is called.
You have to add your view to parent view first. After that you can add constraints.
Try this:
override func viewWillAppear(_ animated: Bool)
{
self.view.addSubview(barView)
configureURLBar()
}
Include your logic in viewDidLayoutSubviews instead of in viewWillAppear...
viewDidLayoutSubviews is a much better place to handle geometry