How can i put a small subview on a view xcode - ios

i have a button in my view and i want that when user pushed it a small view should apper on it. there is two image about what i want.
on this view-->
this view
How can i do tahat

Set up the small view as a top-level object in IB (that is, outside any view controller's view) and connect an outlet to refer to it, or set it up programmatically.
Then, in the action method for your button press, add that view as a subview of your view controller's view.
See the View Programming Guide for more details.

Steps:
1. Create a custome view "this view".
2. On click of a button call
- [self.view addSubview:thisview] if just to show the view over the last view.
- [self.view presentViewController:thisview animated:Yes]
3. To remove the view:
[self.view removeFromParentViewController]

Related

Xcode Storyboard: Can't add View to ViewController

I'm using Xcode 10 (with Swift 5 and for iOS 12) and my app is currently set up like this:
UIViewController 1 (login screen) - segue12 to ->
NavigationController that automatically created
UIViewController 2 with a UITableView - segue23 to ->
UIViewController 3 (detailed info about an item in the UITableView with automatically created "back" button)
The NavigationBar only contains a " ".
I now want to add another UIView with a button below the UITableView but Xcode won't let me drag it below the UITableView, only either into the UITableView or directly underneath "First Responder", which puts the View outside into its own window.
If I delete "My Table View", it won't let me drag in a replacement either.
How do I fix this, so I can add the new UIView + UIButton?
NavigationController that automatically created UIViewController 2
That explains everything. When you drag a navigation controller into the storyboard, what you get is a navigation controller and its root view controller, and the root view controller is a UITableViewController.
Let's step back for a moment. A view controller can and must have exactly one main view. It occupies the whole scene. It cannot be resized. It cannot have a sibling view, because it has no superview for a sibling view to be a child of. It can only have children (subviews).
Well, in this case, ViewController2 is a UITableViewController, and MyTableView is ViewController2’s main view. (A table view controller is always configured this way, and that is what you got when you dragged the navigation controller into the storyboard.) That is why you cannot add more views to it, except as subviews, e.g. prototype cell contents. Your button has no place to go except inside the table view.
So, what to do?
If the extra interface you want to add is just a button, the usual solution is to put it into the navigation bar. It becomes part of the table view controller's navigation item.
If you don't want to do that — that is, if you really want the button and the table view to be siblings of one another in the interface — then you need to replace ViewController2 itself with an ordinary view controller. Now its main view will be an ordinary view, and you can drag anything you like into it, including a table view and a button. More likely, instead of a bare table view, you would use a container view, with an embed segue to a table view controller.

How to add view with buttons (target-action) to a scroll view

I have created a custom UIViewController with its view in a .xib file created in Interface Builder. The view looks like this:
The view has a UIButton with a target-action pair. The action is a method in the view's view controller. I want to add this view to a UIScrollView, so I created a simple custom view controller which just has as UIScrollView in it. I added the first view controller's view as a subview to the scrollview and set the content size properly.
Everything now works fine, except that when I press the button the application crashes and the 'EXC_BAD_ACCESS' warning comes up with error code 2.
How can I solve this?
There is an action in your view controller that you assign to your button ?

loading view controller embedded inside navigation

Im trying to create a loading view controller before it loads my tableView. This works fine.
However when the tableView is displayed I get a back button. When clicked it takes me back to the loading view. Im guessing this is because its embedded inside of the navigation controller. Please advise.
if you want to hide the back button just add
self.navigationController.navigationBarHidden=YES;
in the third View controller's viewDidLoad
else add
self.navigationItem.hidesBackButton=YES;
You have different options to choose from, depending on your needs:
The loading view can push the navigation controller.
You could avoid using a full view controller to do your loading, and instead just set a view above the rest of your "offers view controller".
Or, refers to Raon answers if you just want the button to disappear
The navigation controller will keep all the controllers that you pushed in a navigation stack.
So if you push ladingviewcontroller using navigation controller and then pushing tableview controller the navigation stack will contain both the controllers and thats why on pressing back button, you are navigated to loadingViewController.
What I suggest is to remove loadingviewcontroller and show loading view in your tableviewcontroller before loading the tableview. Like, in viewWillAppear of tableviewcontroller, just add a UIView with loading indicator and add it as a subview of tableviewcontroller and remove it after you are ready to show your tableview.
Well, if you just want to hide the back nav bar button, you can do this
[self.navigationController.navigationItem setHidesBackButton:YES animated:YES];
But the better way to show the loading controller's view would be this,
[self.view addSubview:loadingController.view];
[loadingController willMoveToParentViewController:self];
[self addChildViewController:loadingController];
[loadingController didMoveToParentViewController:self];
just hide it or remove it from superView when loading view is not required. You can even animate it while hiding so that it gives a nicer effect.
Here we have a architecture issue, the best way is the following:
Make the 2 and 3 view a single one. I mean you must add the ActivityIndicator at the center of the view that contains the table view.
Make the table view hidden and startAnimating the UIActivityIndicatorView.
Do all your loading stuff.
When you finish loading, stopAnimating your UIActivityIndicatorView and make the table view visible again.
And that's it ;)
Do not push the offers load view controller via navigationController, just subview its view on the offersviewcontroller (the third one) like this in the viewDidLoad method:
OffersLoadViewController *offerLoadView = [[OffersLoadViewController alloc] initWithNibName:#"OffersLoadViewControllerv" bundle:nil];
[self.view addSubview: offerLoadView.view];
After dealing with loading just remove it:
[offerLoadView.view removeFromSuperview];

How to add a view controller's view behind a current view controller?

I have a navigation controller shows in the screen ,and I want to have another view controller's view just sit behind the current navigation controller (for some use later)
How can I do that ?
thank you
If you want to hide it then
yourView.hidden=YES;
If you want to change view arrangment bring the subview you want to the front
[self.view bringSubViewToFront:yourView];
Alternatively, create it only when you need it

UIViewController as a subview for other view controllers

My app has a menu button which is available in every view controller. Every time a user taps on the menu button, a small menu pops up. The menu has multiple UIButtons, and each button links to another view controller.
My current solution is to create a view controller with a nib for the menu view and add it as a subview to each of the other main view controllers.
Is there is a better solution?
There could be multiple ways of doing it and I don't think there is the best answer.
However, in the performance perspective, implementing a view container such as UINavigationController or UITabBarController would be most effective.
Implement a root view controller (whose view is added as the only direct subview of the application window), and add the menu as a subview of its view. Let the root view controller decide (or know) which view to display, and add the view as a subview of its view, below the menu.
In this way, the view for the menu need not be removed and added again to the current view hierarchy.

Resources