FS.O5 FS.O5 - 3 months ago 9
Swift Question

Present a view controller on top another one without full screen

I have

MainViewController
and
AddViewController
that should be presented when the user clicks on a button in
MainViewController
.

I've tried to use:

self.presentViewController(AddViewController(), animated: false, completion: nil)


but it presents it all over the screen (full screen).

I've searched for some answers in StackOverflow but I found a solution just for iPad.

So how can I present the view in the correct way?

Note: I set the
AddViewController
's view's frame in the
Xib
file.

Answer

You can use AddSubview function. Check this code.

Create a class file:

class MyClass: UIView {
    class func instanceFromNib() -> UIView {
        return UINib(nibName: "nib file name", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as UIView
    }
}

Then you can use this class to grab the view from xib:

var myview = MyClass.instanceFromNib()

And then you can add this view wherever you want:

var presenterStoryBoard:UIStoryboard = UIStoryboard(name: "nameofstoryboard", bundle: nil)
var myview = MyClass.instanceFromNib()

var vc = presenterStoryBoard.instantiateInitialViewController() as MainViewController

publisherViewController.view.addSubview(myview.view)
Comments