Put a view on Storyboard above all others - ios

I have a ViewController file and in this ViewController file, I create programmatically a view which it takes all the available place on the window.
Now, I want to make a view with the Storyboard. I put this view (in my case a button) on my Storyboard.
And when I launch my application, I cannot see my button because it is under the view which I created before programmatically.
Is this possible to have my Storyboard view above the other view ?
Thanks a lot !

Yes, it's possible.
Looks like you use addSubview method.
The problem that you encountered is that views added in such way overlay the existed views.
You can either bring button to front with bringSubviewToFront or use insertSubview:belowSubview and insertSubview:aboveSubview to fix such issues. In both cases you'll need your button stored in property.

It sort of depends on your view hierarchy setup.
I'm assuming this:
A - View controller view
| B - Your button
| C - Programmatically added fullscreen view
In the above case you could do something like this:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view bringSubviewToFront:self.button];
}
Point is bringSubviewToFront: moves a subview to the top of the hierarchy.

So you will have to create an outlet for the button (connect the button in storyboard to the name given to it in your .swift file) Once you have added the view programmatically, you just have to do
self.view.bringSubviewToFront(button)
Assuming that button is the name of the outlet. This line will be added after adding the view programatically.

Related

Bring UIView to the front

I have a TabManViewController (that you can find in this repository https://github.com/uias/Tabman ) which embeds 3 Viewcontrollers.
In one of this embedded ViewControllers I have one UIView that is draggable with a UIPanGestureRecogniser.
When I drag it I see that it goes behind the TabMan Bar, while I want it to go over it.
I tried this snippet of code in the viewController that embeds the view but It doesn't work.
self.view.bringSubviewToFront(cardView)
How can I bring the view above everything else?
You need to add it to either
1- The tabBarController' view
2- Window's view
You have to replace "view" in below code with the parent view of cardview
self.view.bringSubviewToFront(cardView)
self.yourCardview'sParentView.bringSubviewToFront(cardView)

UIScrollView and Storyboard

I am testing out something I would like to have in my app. I have a UiViewController in a storyboard that has a UIScrollView - I now want to add other viewControllers to this scrollView and swipe between them.
I would like to add a view that I made in the storyboard into this UIScrollView. Is it possible?
I tried something along the lines of:
MYViewController *viewOne = [self.storyboard instantiateViewControllerWithIdentifier:#"myView"];
[self.scrollView addSubview:viewOne.view];
I've set the the scrollView size to be bigger than the screen and when the main view loads, I can see there is a scroll view (the scroll bars show) but my viewController is not inside it.
Anyone have any ideas?
The code you posted is still not right, on a couple of levels.
First, you should not use alloc/init for view controllers. You either need to use initWithNibName:bundle: (to create a view controller from a nib file) or instantiateViewControllerWithIdentifier: to load a view controller from a storyboard.
Second, you should not add a view controller's view as a subview of another view controller unless you use the parent/child view controller support that was added in iOS 5 and greatly improved in iOS 6. If you do what you are doing then all sorts of things won't work correctly: Auto-rotation, low memory warnings, background notifications etc. The list of things that can go wrong is unbounded.
The easiest way to do this is to add a container view as a frame to hold your child view controller, and then control-drag from your container view onto the scene that you want to set up as a child. This causes IB to set up and "Embed" segue. Embed segues do all the housekeeping you need to host one view controller's content inside another, with no code needed.
You could create a container view inside your scroll view's content view, and then it would just work fine.
I found the problem:
I was not allocating and initialising my viewController. Ooops.
This is the correct code:
BaseViewController *viewOne = [[BaseViewController alloc]init];
viewOne = [self.storyboard instantiateViewControllerWithIdentifier:#"myView"];
[self.scrollView addSubview:viewOne.view];

Is there any way to place a UIView above a UIPopoverController?

I have code in my app that adds a UIView to the root view controller's view. This view is semi-transparent, and functions as a modal overlay that covers the entire application (the view contains a button that when clicked removes the view from its superview, which is how the user gets back to the main application).
This has worked fine up to now, but now we're using UIPopoverControllers and attempting to use this same modal overlay to block the main application. The problem is that this UIView is shown behind the popover, instead of on top of it.
Is there any way to add a subview to a root view controller's view in such a way that it appears on top of any visible UIPopoverControllers, but without dismissing them?
The reference on UIPopoverController states:
"The popover content is layered on top of your existing content in a special type of window."
NSLogging of the subviews-array shows, that the view is not added to the view hierarchy of the viewController that displays it. From my point of view what you are trying to do isn't possible.
Here is an idea though:
You could add your blocking UIView to the contentViewController of the UIPopOverController and use it to set a property on the contentViewController.
Then you set your main viewController, which actually displays the UIPopoverController, as delegate of the popover and do something like this:
- (void) popoverControllerShouldDismissPopover:(UIPopoverController*)popoverController
{
if(popoverController.contentViewController.yourProperty)
{
return YES;
}
return NO;
}
I don't know how much this helps in your current situation, but maybe it gets you started.

Custom views with Storyboard

In complex screens (View Controllers) I used to separate the whole thing in smaller pieces (I call them widgets). These widgets consist basically of a MyWidget.h and a MyWidget.m file as well as a MyWidget.xib file, where the root element is a UIView and the MyWidget class is the File Owner of the UIView. In the init of this widget I do a loadNibNamed.
In my View Controller I then do a [[MyWidget alloc] init], which I add to View's Controller main view as a sub view. This, so far, works perfectly.
I'm now wondering, how to do the same with storyboard, because I cannot really start to drag in a UIView somewhere, I always have to start with an UIViewController, which I don't want to.
If there is no possible way doing this with a Storyboard, can I simply do it the old way, by using the Storyboard for my main screens and segues, and use a separate .xib file to define custom views?
Putting the widget/view in a separate .xib file works, and is appropriate especially if you might want to reference that same view from multiple View Controllers.
However, sometimes you do want to see the additional view/widget within the same storyboard, and it is possible. Here's how you do it:
Select your view controller in IB (click on the black bar below the view), then drag a UIView from the Object Library into the black bar:
When a view is in the black bar, it's instantiated like any other view in IB but just isn't added to your view hierarchy until you do so in code. Change the view's class to match your own subclass if necessary:
You can hook it up to your view controller like you would hook up any other view:
The added view shows up in your Document Outline and you can hook up actions and references there too:
Now, the problem that remains is that you can't actually see the view no matter how many times you try to click or double click, which would defeat the whole purpose of putting it in the same storyboard. Fortunately there are two workarounds that I know of.
The first workaround is to drag the view from the black bar back into your view controller's view, edit it, then drag it back into the black bar once you're done. This is troublesome but reliable.
The other workaround is more finicky, but I prefer it because it lets me see all my views at the same time:
Drag a UITableView from the Object Library into your newly added view.
Then drag a UITableViewCell into that UITableView.
Once you do that, your view pops out magically by the side, but you have a UITableView that you don't want. You can either resize that to 0x0, or you can delete it and your UIView will (usually) still stay visible.
Occasionally the secondary view will become hidden again in IB. You can repeat the above steps if you deleted the UITableView, or if the UITableView is still in the hierarchy you just need to click on the UITableViewCell and the view will appear again.
The second method works for UIViews but not so well for UIToolbars and is impossible for UIButtons, so the cleanest solution I've found when you need to include lots of different subviews is to attach a single secondary UIView to your view controller as a container that never gets shown, put all your secondary views in there, and use the UITableViewCell trick to make everything visible. I resize my dummy UITableView to 0x0 to make that invisible. Here's a screenshot of how it all looks like together:
If you're just looking to make your view controllers else-where(and not in your story-board), then there's a pretty simple way to accomplish this:
1) Create your CustomViewControllers(abcdController in the code I tried) with their individual xibs as usual.
2) Add a UIViewController(or whatever was the superclass of your CustomViewController) to the story-board.
3) Set the CustomClass to CustomViewController instead of UIViewController as shown here:
4) Finally, in your viewDidLoad, load the custom xib and you're done.
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSBundle mainBundle] loadNibNamed:#"abcdController" owner:self options:nil];
// Do any additional setup after loading the view from its nib.
}
I think you can do something like this to get instance of specific viewcontroller from Storyboard and use view on top of it.
ex:
MyViewController* myViewController = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"myViewController"];
UIView* view = myViewController.view; //Get the view from your StoryBoard.
Hope this helps
Thanks
Vijay

iPhone : UINavigation Controller within a view displaying another view

I think I am having a case of disappearing up my own arse.
I am creating a small view on a ipad thats for settings (so not full scree), within this view. I need a navigation controller to show another view.
At the moment I have one class / xib
The xib contains the main view (graphic / boarder). This view is linked to the files owner and appear.
On the same xib, I also have a navigation controller that contains the inner view.
OnViewDidLoad I add the navigationcontroller.view to the subview and it appears. However I cant push anything off it. I wired up the delegate and etc but I am sure I am missing something stupid
Can I do this all within one controller / xib?
The only code I have done is
[self.view addSubview:mainNavigationController.view];
Is there some code I need to do for the navigationController
Just adding the navigation controller as a subview doesn't hook up the navigation controller to the view controller hierarchy properly. That's probably why it doesn't work.
Also the properties that need to be set are readonly properties, so I don't think there's anything you can really do about it.

Resources