Adding a webview for each tab in UITabBarController? - ios

How do I add a web view for each of these tabs in my UIViewController?
public class TabController: UITabBarController
{
UIViewController tab1, tab2, tab3, tab4, tab5, tab6, tab7;
public TabController()
{
tab1 = new UIViewController();
tab1.Title = "Test";
tab1.View.BackgroundColor = UIColor.Orange;
tab2 = new UIViewController();
tab2.Title = "Test2";
tab2.View.BackgroundColor = UIColor.Orange;
tab3 = new UIViewController();
tab3.Title = "Test3";
tab3.View.BackgroundColor = UIColor.Red;
tab4 = new UIViewController();
tab4.Title = "Test4";
tab4.View.BackgroundColor = UIColor.Red;
tab5 = new UIViewController();
tab5.Title = "Test5";
tab5.View.BackgroundColor = UIColor.Red;
tab6 = new UIViewController();
tab6.Title = "Test6";
tab6.View.BackgroundColor = UIColor.Red;
tab7 = new UIViewController();
tab7.Title = "Test7";
tab7.View.BackgroundColor = UIColor.Red;
var UIViewController = new UIViewController[] {
tab1, tab2, tab3, tab4, tab5, tab6, tab7
};
tab1.TabBarItem = new UITabBarItem(UITabBarSystemItem.Favorites, 0);
ViewControllers = UIViewController;
}
}
So for each tab, instead of "tab1.View.BackgroundColor = UIColor.Orange;", it
should be:
Webview + Title + Link so that each tab has a different page?

The easiest way would be to do this using the storyboard and assigning the viewcontroller classes to your tabs. However if you want to do it programatically I suppose you would need to do the following:
tab7 = new UIViewController();
tab7.Title = "Test7";
tab7.View.BackgroundColor = UIColor.Red;
//Create a webview
UIWebView webView = new UIWebView(View.Bounds); //Takes size as a constructor parameter
//Set it's navigation location
webView.Navigate(new URI("www.whatever.co.uk"));
//Add the webview to the subviews of your UIViewController
tab7.View.AddSubview(webView);
You'd do this for each tab.
On a side note it's worth mentioning that apples guidelines for application submissions to their store does tend to frown upon applications which are just web site representations, so worth bearing that in mind. The application must be able to do more than apples web browser could.

Related

Xamarin iOS Back To Master Button Is not shown on detail view

In short I have a SplitViewController as RootController.
And in constructor I've added master and detail controllers.
Detail Controller is wrapped into Navigation Controller.
{
_masterViewController = new MenuViewController();
_inventories = new InventoriesViewController();
_detailNavigationController = new UINavigationController(_inventories);
ViewControllers = new UIViewController[] {
_masterViewController,
_detailNavigationController
};
What are the ways I can show the Menu (show master) back button on details Navigation Bar?
We don't use storyboards
add the following code in the method ViewDidLoad()
UIButton backButton = new UIButton();
backButton.Frame = new CGRect(0,0,20,20);
backButton.SetTitle("Back", UIControlState.Normal);
backButton.SetTitleColor(UIColor.Gray, UIControlState.Normal);
backButton.SetImage(new UIImage("back"), UIControlState.Normal);
//backButton.SetImage(new UIImage("001.jpg"), UIControlState.Normal);
backButton.TouchUpInside += (sender, e) =>
{
NavigationController.PopViewController(true);
};
UIBarButtonItem backItem = new UIBarButtonItem(backButton);
NavigationItem.LeftBarButtonItem = backItem;
After some time I found the property on the UISplitViewController
DisplayModeButtonItem
So after creation of master and detail controller (detail controller is wrapped into UINavigationController) I added the DisplayModeButtonItem into NavigationBar LeftItem of detailed controller. It gave me the desired look and behaviour
{
_masterViewController = new MenuViewController();
_inventories = new InventoriesViewController();
_inventoriesNavigation = new UINavigationController(_inventories);
_inventories.NavigationItem.LeftBarButtonItem = DisplayModeButtonItem;
ViewControllers = new UIViewController[] {
_masterViewController,
_inventoriesNavigation
};
}

Push more than 3 ViewController in NavigationController from AppDelegate App crash

I am working on Push Notification. Now When the Application is raise to Device and When I Tap on it. I want to push 3 ViewController to Navigation Stack.
So I am using below code to do this.
AppDelegate.cs Code
Window = new UIWindow(UIScreen.MainScreen.Bounds);
Menu = new SlideoutNavigationController();
var storyboard = UIStoryboard.FromName("Main", null);
var webController = storyboard.InstantiateViewController("DashBoardViewController") as DashBoardViewController;
Menu.MainViewController = new MainNavigationController(webController, Menu);
Menu.MenuViewController = new MenuNavigationController(new DummyControllerLeft(), Menu) { NavigationBarHidden = false };
Window.RootViewController = Menu;
Window.MakeKeyAndVisible();
var storyboarddd = UIStoryboard.FromName("Main", null);
var webControllerdd = storyboarddd.InstantiateViewController("DashBoardViewController") as DashBoardViewController;
webControllerdd.reloadNotication();
UINavigationController nav = webController.NavigationController;
var notifyWebController = storyboard.InstantiateViewController("NotificationListViewController") as NotificationListViewController;
notifyWebController.navigationContoller = nav;
nav.PushViewController(notifyWebController, true);
if (type.Equals("Damage Report"))
{
var webController2 = storyboard.InstantiateViewController("DamageReportViewController") as DamageReportViewController;
webController2.DamageReportId = id;
webController2.navigationContoller = nav;
nav.PushViewController(webController2, true);
}
if (type.Equals("Overloss"))
{
var webController2 = storyboard.InstantiateViewController("OverlossViewController") as OverlossViewController;
webController2.PacketId = id;
webController2.navigationContoller = nav;
nav.PushViewController(webController2, true);
}
The upper code is working fine to open Specific ViewController.
But My App crash after it with the crash Log.
Crash Report :
2017-07-26 15:25:18.330 Aastha.iOS[6357:2021514] nested push animation can result in corrupted navigation bar
2017-07-26 15:25:18.740 Aastha.iOS[6357:2021514] Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.
My Solution :
When i search on google and SO someone says that Open ViewController one after another from each ViewController ViewDidAppear method but I am not sure If this is correct way.
Any Help be Appreciated.
You can't push more than one view controller with animation. If you want user to see transtions A -> B -> C you indeed have to do it in A's and B's viewDidAppear method.
If you want user to only see the last view controller C and be able to go back through A and B, you can push them all at once, but you need to do it without animation:
nav.PushViewController(webController2, false);
Another option is to use SetViewControllers like this:
nav.SetViewControllers([A, B, C], true);
this will push all 3 view controllers at the same time and only the topmost one will be animated using push transition. I don't think this fits your situation, just adding it here for completeness sake.

DialogViewController not showing navigationbar

I am instantiating a DialogViewController within a MvvmCross View like this
var myDialog = new MyDialogViewController();
var navController = new UINavigationController();
controller.NavigationBar.TintColor = UIColor.Black;
controller.PushViewController(myDialog, false);
Add(controller.View);
And this is how the MyDialogViewController looks like:
public class MyDialogViewController : DialogViewController
{
public MyDialogViewController()
: base(UITableViewStyle.Grouped, new RootElement("MyRoot"), true)
{
var root = new RootElement("MyRoot") {
new Section
{
new HtmlElement("MyWebsite", https://www.mywebsite.com")
}
};
Root.Add(root);
}
}
The dialog appears fine with the NavigationBar but if I select the Html element MyWebsite the Webview is displayed but without the NavigationBar and I am not able to navigate back.
The same thing occurs for elements that require to navigate to a new window, the navigationbar is not shown.
Any idea how to make the NavigationBar show after navigating to the WebView?
This worked for me:
var settings = new SettingsDialogViewController((SettingsViewModel)ViewModel, new RootElement("Settings"));
var window = UIApplication.SharedApplication.KeyWindow;
navigationController = window.RootViewController.ChildViewControllers[1].NavigationController;
navigationController.PushViewController(settings, true);
navigationController.NavigationBarHidden = false;

UITabBarController wait loading UIViewController until selected Xamarin

Is it possible to call a method when a new UIViewController is selected in a UITabBarController ? Or is it possible to wait loading the whole UIViewController until the view is selecte?
I have a UITabBarController that looks like:
public sealed class VisualizationViewController : UITabBarController
{
private UIViewController tabVand, tabVarme, tabEl;
private UIToolbar toolbar;
public VisualizationViewController()
{
toolbar = new UIToolbar(new RectangleF(0, 0, (float)View.Frame.Width, 60));
toolbar.AutoresizingMask = UIViewAutoresizing.FlexibleWidth;
View.AddSubview(toolbar);
tabVand = new WaterVisualizationViewController();
tabVand.Title = "Vand";
tabVand.TabBarItem = new UITabBarItem("Vand", UIImage.FromFile("Images/Water.png"), 0); // Tag == initial order
tabVand.TabBarItem.SetFinishedImages( UIImage.FromFile("Images/Water.png"), UIImage.FromFile("Images/Water.png"));
tabVand.View.BackgroundColor = UIColor.LightGray;
tabVarme = new TemperatureViewController();
tabVarme.Title = "Varme";
tabVarme.TabBarItem = new UITabBarItem("Varme", UIImage.FromFile("Images/Heat.png"), 1); // Tag == initial order
tabVarme.TabBarItem.SetFinishedImages(UIImage.FromFile("Images/Heat.png"), UIImage.FromFile("Images/Heat.png"));
tabVarme.View.BackgroundColor = UIColor.LightGray;
tabEl = new ElVisualizationViewController();
tabEl.Title = "EL";
tabEl.TabBarItem = new UITabBarItem("EL", UIImage.FromFile("Images/Power.png"), 2); // Tag == initial order
tabEl.TabBarItem.SetFinishedImages(UIImage.FromFile("Images/Power.png"), UIImage.FromFile("Images/Power.png"));
tabEl.View.BackgroundColor = UIColor.LightGray;
var tabs = new[]
{
tabVand, tabVarme, tabEl
};
ViewControllers = tabs;
SelectedViewController = tabVand;
}
And right now, it loads all the Views, which is going to be an expensive call, when I move from dummy data to the real data each view should load. So I want to only make a data call when the view is selected in the UITabBarController.
So what's happening is that your ViewDidLoad() method is firing (assuming you're calling your webservice there) for each tab view controller in your constructor. Each View Did Load is firing because you're calling View.Background, which will load the view into memory. You don't want to do that here, but in the ViewDidLoad() method of each individual tab View Controller:
public sealed class VisualizationViewController : UITabBarController
{
private UIViewController tabVand, tabVarme, tabEl;
private UIToolbar toolbar;
public VisualizationViewController()
{
base.ViewDidLoad();
toolbar = new UIToolbar(new RectangleF(0, 0, (float)View.Frame.Width, 60));
toolbar.AutoresizingMask = UIViewAutoresizing.FlexibleWidth;
View.AddSubview(toolbar);
tabVand = new WaterVisualizationViewController();
tabVand.Title = "Vand";
tabVand.TabBarItem = new UITabBarItem("Vand", UIImage.FromFile("Images/Water.png"), 0); // Tag == initial order
tabVand.TabBarItem.SetFinishedImages( UIImage.FromFile("Images/Water.png"), UIImage.FromFile("Images/Water.png"));
//Don't do this, it will make its ViewDidLoad fire
//and you don't want that.
//tabVand.View.BackgroundColor = UIColor.LightGray;
tabVarme = new TemperatureViewController();
tabVarme.Title = "Varme";
tabVarme.TabBarItem = new UITabBarItem("Varme", UIImage.FromFile("Images/Heat.png"), 1); // Tag == initial order
tabVarme.TabBarItem.SetFinishedImages(UIImage.FromFile("Images/Heat.png"), UIImage.FromFile("Images/Heat.png"));
//tabVarme.View.BackgroundColor = UIColor.LightGray;
tabEl = new ElVisualizationViewController();
tabEl.Title = "EL";
tabEl.TabBarItem = new UITabBarItem("EL", UIImage.FromFile("Images/Power.png"), 2); // Tag == initial order
tabEl.TabBarItem.SetFinishedImages(UIImage.FromFile("Images/Power.png"), UIImage.FromFile("Images/Power.png"));
//tabEl.View.BackgroundColor = UIColor.LightGray;
var tabs = new[]
{
tabVand, tabVarme, tabEl
};
ViewControllers = tabs;
SelectedViewController = tabVand;
}
Then in each individual tab (UIViewController) implementation, call your web service in it's ViewDidLoad() method, and not in the constructor. Example:
public class WaterVisualizationViewController : UIViewController
{
public WaterVisualizationViewController()
{
//Don't call your web service here
}
public override ViewDidLoad()
{
//Set your background color here
//Call your web service
//Populate your UI with data
}
}
Now if your web service calls are expensive, you might see a little lag when tapping the tabs. For that you should consider using UIActivityIndicatorView to display a "Loading Data" type of indicator so that the user has feedback your app is doing something and not frozen. Alternatively, you can use BTProgressHUD from the Xamarin Component Store, which is really easy to use

how to use flyout navigation in xamarin IOS

I'm new to Xamarin IOS and I have a project which needs this flyout navigation.
var navigation = new FlyoutNavigationController {
// Create the navigation menu
NavigationRoot = new RootElement ("Navigation") {
new Section ("Menu") {
new StringElement ("Recipe"),
new StringElement ("Recipe Basket"),
new StringElement ("Meal Planner"),
new StringElement ("Grocery List"),
new StringElement ("Social"),
new StringElement ("Videos"),
new StringElement ("News Feed"),
new StringElement ("Partners"),
}
},
// Supply view controllers corresponding to menu items:
ViewControllers = new [] {
new UIViewController { View = new UILabel { Text = "Animals (drag right)" } },
new UIViewController { View = new UILabel { Text = "Vegetables (drag right)" } },
new UIViewController { View = new UILabel { Text = "Minerals (drag right)" } },
},
};
This code just presents the UILabel. How can I do this to send it to a specific view controller?
Please help.
Thanks in advance.
Do what vinaykumar said, but you also want to open the FlyoutNavigationController from those UIViewControllers, so you have to pass that initial one you created to those controllers.
Otherwise if you just open your view controller, you won't be able to get the menu again.
I'm opening navigation controllers like so:
MainViewController in ViewDidLoad:
UIStoryboard board = UIStoryboard.FromName("MainStoryboard_iPhone", null);
UINavigationController nav = (UINavigationController)board.InstantiateViewController("nav");
SearchViewController searchVC = (SearchViewController)nav.ViewControllers[0];
// Passing flyoutnavigation here
searchVC.navigation = navigation;
SettingsViewController settingsVC = (SettingsViewController)board.InstantiateViewController("settings");
UINavigationController navSettings = new UINavigationController(settingsVC);
// Passing flyoutnavigation here
settingsVC.navigation = navigation;
navigation.ViewControllers = new[]
{
nav,
navSettings
};
In the other view controllers, (Settings and Search in my example), I have a public variable to store the FlyoutNavigationController (set in MainViewController) then add a button to open the FlyoutNavigationController that was passed:
// Set in MainViewController
public FlyoutNavigationController navigation;
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Add button to allow opening the FlyoutNavigationController
NavigationItem.LeftBarButtonItem = new UIBarButtonItem(UIBarButtonSystemItem.Action, delegate
{
navigation.ToggleMenu();
});
}
you have to add the FlyoutNavigationController's View to your View
// Show the navigation view
navigation.ToggleMenu ();
View.AddSubview (navigation.View);
This code just presents the UILabel.
because you are only providing UILable as a view to your new UIViewController.
View = new UILabel { Text = "Animals (drag right)" }
How can I do this to send it to a specific view controller?
Provide your UIViewController instead of new UIViewController
for instance change the following code
ViewControllers = new [] {
new UIViewController { View = new UILabel { Text = "Animals (drag right)" } },
new UIViewController { View = new UILabel { Text = "Vegetables (drag right)" } },
new UIViewController { View = new UILabel { Text = "Minerals (drag right)" } },
},
to
ViewControllers = new [] {
new <your_UIViewController>(),
new <your_UIViewController>(),
new <your_UIViewController>(),
},
Hope this helps you, let me know if you faced any issue while implementing the same.

Resources