swift move UIButton up in VC stack - ios

I wonder how I can move a UIButton up in the VC stack?
My problem:
In my VC I am adding a page controller view in order to swipe between images. But the problem is when I do that the button gets stuck behind the added page controller view.
My VC looks like:
My code when adding the page view controller:
self.pageViewController.setViewControllers(viewControllers as [AnyObject], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)
self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)
self.addChildViewController(self.pageViewController)
self.view.addSubview(self.pageViewController.view)
self.pageViewController.didMoveToParentViewController(self)
This will result in the button not showing / stuck behind the page view controller.
I've already tried to add a second UIView under the root view and the button before that view then adding the page view controller as subview to that new view by : self.MyNewSubView.addSubview(self.pageViewController.view)
That almost works. The button is showing in front of the page view controller but the autolayout gets messed up for the page view controller since it containts a uiscroll view with a image (zoom in/out).
I also tried to set the CGRectMake to eg CGRectMake(0,50) that kinda works. But problem there is big images in portrait getting clipped at the bottom.
So what I would want to do is to pushing the button up the stack once I added the page view controller.
Thanks in advance,

You can ask your current view to bringSubviewToFront(button)

Related

Adding subview to UINavigationController causes rootViewController to disappear on modal dismissal

I have a view controller in a custom UINavigationController (I have only added a CAGradientLayer to the background). The view controller has a collection view with IGListKit in which a cell can present a view that I add as a subview to the navigation controller. Up until there everything works as expected but when I present a new modal view controller by tapping a UIBarButtonItem and close it with
self.dismiss(animated: true, completion: nil)
The navigation controller is suddenly empty and the root view controller that presented it is no longer in the view hierarchy. So I truly have no idea why adding the subviews to the navigation controller is causing this and how I can fix it. Thank you!

Swift - Without storyboard , how do I make a modal popup?

I didn't use a storyboard and I'm having trouble finding resources on programmatically making a modal popup ( it'll be a form with white background that takes up the bottom half of the screen) after you press a button. The previous view would be disabled until you exit out of this modal popup.
Would this modal popup have its own view and view controller? How would I make the popup appear and after you exit, you give the control back to the original view controller?
Thanks?
let rateViewController = RatePopupVC()
rateViewController.modalTransitionStyle = .crossDissolve
rateViewController.modalPresentationStyle = .overCurrentContext
rateViewController.item = item // In case you want to transfer some data
self.present(rateViewController, animated: true, completion: nil)
Would this modal popup have its own view and view controller
Yes. Configure your view controller and its view, and present it with present and dismiss it with dismiss. You can add custom transition animation and custom positioning of the view.

How to present a UIViewController on top of my current UIViewController?

I'm doing everything programmatically. Not using storyboards.
I have a navigation bar at the bottom of my screen (like a tab bar, but custom made), with 5 options. I'd like to present a different view controller when each of those navigation options is clicked. However, I don't want the presented view controllers to cover my navigation bar at the bottom.
Ex: I have a settings button that can be pressed. I also have a SettingsViewController file. When a user clicks on the 'settings' button, I do
let settingsViewController = SettingsViewController()
settingsViewController.modalPresentationStyle = .OverCurrentContext
self.presentViewController(SettingsViewController(), animated: false, completion: nil)
but this covers my entire screen with a new view. When I change the color of this new view to UIColor.clearColor(), I can still see my navigation bar at the bottom but I cannot interact with it (the new view is on top of it).
I have tried to change the frame of my SettingsViewController view so that it does not cover the entire screen by putting
self.view.frame = CGRectMake(0,0,width,height-100)
in viewDidLoad() in my SettingsViewController. But the view still covers the entire screen!
I want my SettingsViewController to display only from the [top to 100px from the bottom] area of the screen. I want my navigation bar to remain in the bottom 100px of the screen. How can I do this?
Figured it out :)
I was trying to present my new SettingsViewController when all I needed to do was add the view from my SettingsViewController as a subview.
The solution is to do
self.view.addSubview(settingsViewController.view)

iPhone, iOS 8: How to presentViewController smaller than original view controller?

I have two view controllers, I want to present view controller(VC) of the first VC. Second VC have smaller size. I want to show second view controller over the first one. Like a popover. You can imagine it, if we add another view controller that slides from bottom to top but stops at navigation bar.
Here is my code:
#IBAction func showSecondVC(sender: AnyObject) {
let secondViewController = storyboard?.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController
secondViewController.view.frame = CGRect(origin: CGPoint(x: view.frame.origin.x, y: header.frame.height), size: CGSize(width: view.frame.width, height: view.frame.height - header.frame.height))
secondViewController.definesPresentationContext = true
secondViewController.providesPresentationContextTransitionStyle = true
secondViewController.modalPresentationStyle = UIModalPresentationStyle.CurrentContext
self.presentViewController(secondViewController, animated: true, completion:nil)
}
You can see that I set frame for secondViewController. I want that it will be with this frame, but if I add completion block and show it's frame after animation, it will be same as first View Controller have.
Edit
I also want to mention, that I try to make this in Portrait orientation.
If you try this in iPhone, the built-in root view of presented view controller will always be full screen, so your efforts to resize it will not be successful. (iPad is a different story)
Just let the root view as it is (i.e. don't fight the fact it's full screen size ), but make its background color clear color. If you want to present some kind of customised view/content/whatever it is..for example a smaller view with some warning..or some options. Just add it as a subview of the root view.
UPDATE:
As the UIViewController class documentation says:
"In a horizontally compact environment, the presented view is always full screen."
The only combination I can imagine for this to work is Iphone6+ in landscape mode where the horizontal size class is then larger then compact. So you are out of luck because you want portrait.
To add the Earl Grey's response, after setting the ViewController's background color to clear. Create a segue with the following properties set:
Kind to "Present Modally"
Presentation to "Over Current Context"

Views not displaying correctly with storyboard

I created a page based application which already has several views. RootViewController is responsible for setting up the app layout, and it does everything well except for one thing, the view size.
Here's part of what I have
// Configure the page view controller and add it as a child view controller
self.pageViewController = UIPageViewController(
transitionStyle: .Scroll,
navigationOrientation: .Horizontal,
options: nil
);
self.pageViewController.delegate = self;
// Setup the initial page
let startingViewController = self.modelController.viewControllerWithProfileProvider(
self.profileProviderPreference(),
storyboard: self.storyboard
);
self.pageViewController.setViewControllers(
[startingViewController as BrowsingViewControllerBase] as [AnyObject],
direction: UIPageViewControllerNavigationDirection.Forward,
animated: false,
completion: { done in }
);
self.pageViewController.dataSource = self.modelController;
self.addChildViewController(self.pageViewController);
self.view.addSubview(pageViewController.view);
// Set the page view controller's bounds using an inset rect so that self's view is visible around the edges of the pages
self.pageViewController.view.frame = self.view.bounds;
// Notify the page view controller that we just moved to it. This is a framework imposition. See the docs.
self.pageViewController.didMoveToParentViewController(self);
// Add the page view controller's gesture recognizers to the view controller's view so that the gestures are started more easily.
self.view.gestureRecognizers = self.pageViewController.gestureRecognizers;
All other code in the controllers handles behaviour, not aspect, so I setup everything else using Storyboard. The viewControllerWithProfileProvider: method is returning the proper view, which gets presented on screen, but not correctly. Here's what I have in storyboard:
I then change the colour and label for the button according to the provider given (ie., Facebook, Twitter, LinkedIn...). But I get this:
Notice how the button is off the screen. I would assume that self.pageViewController.view.frame = self.view.bounds; is the key line for this, but I'm not sure what to use. How can I have my views display correctly?
Control Click the box and drag up and leave from the popup select Top space to top layout.
do the same on both right and left side and select leading space and trailing space respectively. Run your app hopefully this helps
I think you need to use , I suffered with the same But Autolayout Rescue me from this.

Resources