open viewcontroller from tab like in instagram camera page on IOS
Edit: In order for this code to work you'll need to subclass UITabBarController and implement the UITabBarDelegate. So you'll add something like this:
@interface MyTabsViewController : UITabBarController <UITabBarDelegate>
In Interface Builder you'll need to set the tags of your tab items to whatever you need them to be:
And now you can do something like this:
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
if(item.tag == 1)
{
[self presentViewController:myVC animated:YES completion:nil];
}
else if(item.tag == 2)
{
//your code
}
}
This will allow you to get the tapped event for each of your tab bar items and do add some custom code, you just need to add the appropriate tags to your tab buttons in interface builder. For example, you could add code to show your own view controller as shown above.
Hope that helps!
And here's another link: How can i get Tabbar Item Button's Click event by clicking on TabbarItem button?
Present a View Controller modally from a tab bar controller
I suggest to create a custom class for your TabBarController, then assign the delegate to that.
You can either assign and check the restorationIdentifier
of the view controller, or do a type check. I usually use storyboard identifier as the restoration identifier of the view controller(s).
class TabBarController: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
}
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if let identifier = viewController.restorationIdentifier, identifier == "cameraVC" {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "cameraVC") as! CameraViewController
present(vc, animated: true, completion: nil)
return false
}
return true
}
}
Here's a sample you can play with:
https://gist.github.com/emrekyv/3343aa40c24d7e54244dc09ba0cd95df
Present a View modally from a tab bar controller
I've had to implement something similar in an app I'm currently building, it's relatively straightforward to do, you need to implement a delegate method of UITabBarController
in order to achieve this.
The delegate method you need to implement is: tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool
Returning false from this method will stop the tab controller from selecting your tab, you then just need to implement your own logic to present the UIViewController
programatically.
Here's an example:
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
// If your view controller is emedded in a UINavigationController you will need to check if it's a UINavigationController and check that the root view controller is your desired controller (or subclass the navigation controller)
if viewController is YourViewControllerClass {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let controller = storyboard.instantiateViewController(withIdentifier: "storyboardID") as? YourViewControllerClass {
controller.modalPresentationStyle = .fullScreen
self.present(controller, animated: true, completion: nil)
}
return false
}
// Tells the tab bar to select other view controller as normal
return true
}
I've not tested the above code as my implementation is slightly different and has more variables. The general principle is the same.
Let me know how you get on and I'll update the answer if necessary.
how to open camera by clicking tab in tab bar in iphone sdk?
Try this code snippet.
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
if (([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]))
{
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController: picker animated:YES];
}
else
{
// Put alert no camer found
}
[picker release];
Camera view hides TabView
If you want to see all tabs over camera view, there is only one workaround but it doesn't match what you showed in your first screenshot excatly.
To see all tabs, just add safeAreaInset(edge:alignment:spacing:_:)
modifier right below CameraPreview(camera: model)
and set ignoresSafeArea
to .top
.
Here is the complete sample code:
CameraPreview(camera: model)
.safeAreaInset(edge: .bottom, alignment: .center, spacing: 0) {
Color.clear
.frame(height: 0)
.background(Material.bar)
}
.ignoresSafeArea(.all, edges: .top)
The result:
Hope it will save you.
Navigating another screen when tabbar selected when user already logged in another tab-iOS
1.Is your loginViewController
is embedded inside UINavigationController
?
-If not than embed it inside UINavigationController
2.Create a Push Segue from loginViewController
to AccountViewController
e.g. segue identifier: segueLoginToAccount
3.Now when user click on 3rd tab bar item, create a check in viewDidLoad
on loginViewController
, to see whether user has already logged in or not:
-(void)viewDidLoad {
[super viewDidLoad];
if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"SigninStatus"] isEqualToString:@"SigninSuccess"]) {
//already logged in, push to Account View
[self performSegueWithIdentifier:@"segueLoginToAccount" sender:nil];
}//else user will stay on Login View
}
No need to use tabBarController didSelectViewController:
method.
Open URL in specific TabBar
Add to your UITabBarController extension with UITabBarControllerDelegate, also set to your Tab 4 restoration Id
in TabBarViewContoller
add:
import UIKit
class TabBarViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
// Do any additional setup after loading the view.
}
}
extension TabBarViewController: UITabBarControllerDelegate {
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if viewController.restorationIdentifier == "safariTabViewController" {
if let url = URL(string: "your_url"), UIApplication.shared.canOpenURL(url) {
UIApplication.shared.openURL(url)
}
return false
}
return true
}
}
when you returning false in delegate - you make sure that tab will not be switched
Related Topics
iOS Facebook Login "Given Url Is Not Allowed by the Application Configuration"
How to Take a Snapshot of a Uiview That Isn't Rendered
Declaring Global Variables in Swift
Taking Photo with Custom Camera Swift 3
Facebook Sdk - iOS - Fail to Share Url (Error 102)
Binding Viewmodel and Textfields with Swiftui
Private VS. Fileprivate on Declaring Global Variables/Consts in Swift3
Avplayer with Playback Controls of Avplayerviewcontroller
Multiple Localized .Strings Files in iOS App Bundle
Could Not Cast Value of Type 'Uiview' (0X112484Eb0) to 'Skview' (0X111646718)
iOS 11 Animated Gif Display in Uiimageview
Error Using Swift - Instance Member Cannot Be Used on Type 'Viewcontroller'
Swift- Custom Uitableviewcell Delegate to Uiviewcontroller Only One Protocol Works