0

I've a ViewController like PopUp but the View not stay in position that I wants.

I want that this View stay on the center of screen. The below screen show the View in storyboard with the constraints seted and the result on simulator.

enter image description here

AlertViewController.swift

class AlertViewController : UIViewController {

    @IBOutlet weak var frameView: UIView!
    @IBOutlet weak var btCancel: UIButton!
    @IBOutlet weak var lblInfo: UILabel!

    var delegate : AlertViewControllerDelegate?

    override func viewDidLoad() {
        super.viewDidLoad()        
        self.frameView.layer.cornerRadius = 10
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    }

    @IBAction func cancel(_ sender: Any) {
        delegate?.cancelBt()
        self.dismiss(animated: true, completion: nil)
    }
}

MainViewController.swift (show)

let alertController = self.storyboard?.instantiateViewController(withIdentifier: "AlertVC") as! AlertViewController
            alertController.providesPresentationContextTransitionStyle = true
            alertController.definesPresentationContext = true
            alertController.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
            alertController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
            alertController.delegate = self

            self.present(alertController, animated: true, completion: {})

EDIT

I tried this.

enter image description here

The result: (I did change the background color).

enter image description here

Augusto
  • 3,825
  • 9
  • 45
  • 93

2 Answers2

0

Your problem is setting top , bottom , leading and trailing constraints with static values that may break when you run in device , you need to set

width,height,centerX,centerY

* good practise to make width & height proportional to screen with multiplier or content size dependent *

top label

top , centerX

activity

top to label , centerX

mid label

centerX,Y

cancel button

centerX , bottom

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
0

Your issue doesn't seem to be with the constraints of the content for your alert, but with the placement/constraints of the view that you're presenting in respect with your Parent View. It would seem that you'd need to set your alert-viewer's height-width values and x,y coordinates manually in code to make them present correctly.

[EDIT] I just noticed the order that your constraints are set. Make sure you're dragging from your FrameView to your SuperView and setting the constraint in that direction. That is, FIRST ITEM: FrameView.... SECOND ITEM: SuperView... That might also be a reason for your troubles.

There's also a good discussion on some issues with auto-layout and PresentViewController here: Auto layout invalid after presenting view controller (and many situation)

Either way, I would recommend using Center Horizontally and Center Vertically as opposed to fixed-static constraint values for your 'inner view' in your AlertView. This seems to work better when scaling accross different sized displays.

Some resources you might find useful:

But, seeing as what you're presenting is a rather simple alertview, I'd recommend skipping the re-invention of the wheel and making use of some ready-made libraries that will make your life alot easier.

1.EZAlertController if all you need are text and action buttons.

2.MRProgress if all you need are loading/progress indicators.

3.NYAlertView if what you are looking for is the freedom to add your own UIView inside the alert.

These libraries take care of the 'pesky' constraint issues for you and make development a lot quicker.

[EDIT] You might want to take a look at: https://github.com/vsouza/awesome-ios#alert--action-sheet which has an excellent curated list of useful pods that really help with iOS development.

akseli
  • 1,416
  • 14
  • 22