Add icons to menu items in Evolve FlyoutNavigationController in Monotouch - ios

I have the Evolve Flyout Navigation Controller working on my iOS app but i'm not sure how to add icons to this code. I want an icon to display to the left of each menu item and be unique to the menu item. Here's my code:
// Subclass the component to keep the configuration in one place
using FlyoutNavigation;
namespace Mobile.iOS7
{
public class EvolveFlyoutNavigationController : FlyoutNavigationController
{
string[] MenuTasks = {
"Menu Item One",
"Menu Item Two",
"Menu Item Three",
"Menu Item Four",
};
UINavigationController notImplementedVC;
public EvolveFlyoutNavigationController () : base ()
{
// Create the navigation menu
var menuSection = new Section ();
foreach(var page in MenuTasks)
menuSection.Add(new StyledStringElement (page) { BackgroundColor = MyColors.DarkGray, TextColor = UIColor.White, Font = UIFont.FromName("Lato-Black", 16f) });
NavigationRoot = new RootElement ("Menu") {
menuSection
};
List <UINavigationController> vcList = new List<UINavigationController> ();
var mainSB = UIStoryboard.FromName ("MainStoryboard", null);
vcList.Add(new UINavigationController (mainSB.InstantiateViewController ("DashboardViewController") as DashboardViewController));
vcList.Add(new UINavigationController (mainSB.InstantiateViewController ("LocatorTabController") as LocatorTabController));
ViewControllers = vcList.ToArray ();
NavigationTableView.BackgroundView = new UIImageView (new UIImage ("bg-nav.png"));
NavigationTableView.SeparatorColor = UIColor.LightGray;
}
}
}
Any help is much appreciated!

The menu is being draw with MonoTouch.Dialog, using a StyledStringElement. This element supports an ImageUri property you can use to specify an image to display:
menuSection.Add(new StyledStringElement (page) {
BackgroundColor = MyColors.DarkGray,
TextColor = UIColor.White,
Font = UIFont.FromName("Lato-Black", 16f),
ImageUri = new Uri(__path_to_your_image__)
});

Related

UIPickerView not interactive

I have an application that from the main app "hamburger" menu, if an option is selected I want to show a PickerView for the user to select a number from.
Because it's needs to be accessible throughout the app from this menu I built the UIPickerView in the AppDelegate.cs (as that's where the UINavigationController code is and from that the menu).
Everything shows up correctly: User selected menu button -> menu displays -> User selects "Show Picker" button -> Picker displays with all items. But once the picker displays, you can't scroll the options, nor does the "Done" button I've added register clicks. In fact text fields from the ViewController behind this popup can be clicked on through the popup.
I'm not sure why this UIPickerView is non interactive, does anyone have some thoughts?
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
// create a new window instance based on the screen size
window = new UIWindow(UIScreen.MainScreen.Bounds);
// instantiate the navigation controller
nav = new UINavigationController(new SplashController());
vwNav = new UIView(nav.NavigationBar.Bounds);
var pickerView = new UIPickerView(new CGRect(275f, (UIScreen.MainScreen.Bounds.Size.Height / 2) - (375f / 2), 275f, 375f));
pickerView.ShowSelectionIndicator = true;
var myPickerViewModel = new PickerViewModel(itemList);
pickerView.Model = myPickerViewModel;
myPickerViewModel.PickerChanged += (sender, e) => {
var temp = myPickerViewModel.SelectedItem;
};
// Set up toolbar
var toolbar = new UIToolbar();
toolbar.SizeToFit();
toolbar.Hidden = false;
UILabel titleLabel = new UILabel();
titleLabel.Text = "Select an Item";
titleLabel.Frame = new RectangleF(75, 13, 200, 20);
UIButton doneButton = new UIButton(UIButtonType.Custom);
doneButton.Frame = new RectangleF(40, 335, (float)200, 30);
doneButton.SetTitle("Done", UIControlState.Normal);
doneButton.TouchDown += (sender, e) =>
{
pickerView.Hidden = true;
};
toolbar.AddSubview(titleLabel);
toolbar.AddSubview(doneButton);
pickerView.AddSubview(toolbar);
pickerView.Hidden = true;
btnMenu = new UIButton(UIButtonType.Custom);
btnMenu.Frame = new CGRect(vwNav.Frame.Right - 45, 0, 45, nav.NavigationBar.Bounds.Height);
btnMenu.SetImage(imgMenu, UIControlState.Normal);
btnMenu.SetImage(imgMenu, UIControlState.Selected);
btnMenu.TouchUpInside += (object sender, EventArgs e) =>
{
UIAlertView alert = new UIAlertView();
alert.Title = "Settings";
alert.AddButton("Open PickerView");
alert.AddButton("Exit");
alert.Dismissed += delegate (object alertSender, UIButtonEventArgs args)
{
if (args.ButtonIndex == 0)
{
pickerView.Hidden = false;
}
else
return;
}
}
vwNav.AddSubviews(btnMenu, pickerView);
nav.NavigationBar.Layer.BorderWidth = 2f;
nav.NavigationBar.Layer.BorderColor = (UIColor.FromPatternImage(imgNavBar)).CGColor;
nav.NavigationBar.AddSubviews(vwNav);
// If you have defined a root view controller, set it here:
this.window.RootViewController = nav;
this.window.MakeKeyAndVisible();
return true;
}
I removed some of the unrelated code (a few nav.PushViewController() for different screens and other menu options) to keep it as clear as I could.
From your code, the frame of vwNav is nav.NavigationBar.Bounds, and the frame of pickerView is new CGRect(275f, (UIScreen.MainScreen.Bounds.Size.Height / 2) - (375f / 2), 275f, 375f), if you add pickView to vwNav, the pickerView is out of bounds of vwNav.
Remember that views don't receive touch events where they're outside the bounds of their superview.
That's the cause of your issue.
Solution:
I don't think you should built the UIPickerView in the AppDelegate.cs, create it in your ViewController instead.
You can also add buttons to Navigationbar in ViewController :
UIBarButtonItem btn = new UIBarButtonItem();
btn.Image = UIImage.FromFile("Image");
btn.Clicked += (sender, e) => { System.Diagnostics.Debug.WriteLine("show picker"); };
NavigationItem.RightBarButtonItem = btn;
And remember to add picker to the View of Viewcontroller.
Refer: uinavigationitem and add-uibarbuttonitem-to-navigation-bar-in-xamarin-ios
Feel free to ask me any question:).

View under Navbar bug

I'm using Xamarin Auth via dependency services which presents a new view where the user authenticates However, when the modal page appears and is fully visible, it jumps up and down revealing this black bar
It quickly disapears and the page loads like so: where the top of the page is hidden. Thanks in advance
I've tried the following:
UIWindow window = UIApplication.SharedApplication.KeyWindow;
UIViewController viewController = window.RootViewController;
UIViewController AuthVC = auth.GetUI();
AuthVC.EdgesForExtendedLayout = UIRectEdge.None;
AuthVC.ExtendedLayoutIncludesOpaqueBars = false;
AuthVC.AutomaticallyAdjustsScrollViewInsets = false;
viewController.EdgesForExtendedLayout = UIRectEdge.None;
viewController.ExtendedLayoutIncludesOpaqueBars = false;
viewController.AutomaticallyAdjustsScrollViewInsets = false;
viewController.PresentViewController(AuthVC, true, null);
The UIViewController from Auth.GetUI() comes from this package: https://github.com/xamarin/Xamarin.Auth
Here is my temp fix, It still flashes but it looks correct after if has loaded. Does anyone know what would cause the black bar flashing?
viewController.PresentViewController(AuthVC, true, new Action(() =>
{
var MainUIView = AuthVC.View.Subviews[0];
var MainNavBar = AuthVC.View.Subviews[1];
var NavBarHeight = MainNavBar.Frame.Y + MainNavBar.Frame.Height;
MainUIView.Frame = new CoreGraphics.CGRect(0, NavBarHeight, MainUIView.Frame.Width, MainUIView.Frame.Height - NavBarHeight);
}));
Try this one
viewController.NavigationController.PresentViewController(AuthVC, true, new Action(() =>
{
var MainUIView = AuthVC.View.Subviews[0];
var MainNavBar = AuthVC.View.Subviews[1];
var NavBarHeight = MainNavBar.Frame.Y + MainNavBar.Frame.Height;
MainUIView.Frame = new CoreGraphics.CGRect(0, NavBarHeight, MainUIView.Frame.Width, MainUIView.Frame.Height - NavBarHeight);
}));

Xamarin Forms - Listview on selected item change all background to the same

I found a error when you select a item all the children backgrounds are change for the same color.
In all elements, I put the property BackgroundColor. This only happened in the iOS
Follow the sample of the code:
XAML
<ListView
x:Name="ListPainel"
SeparatorColor="#d2d8e2"
SeparatorVisibility="Default"
Margin="0"
ItemsSource="{Binding ListPainel_Source}"
HasUnevenRows="true"
RefreshCommand="{Binding ListPainel_RefreshCommand}"
IsRefreshing="{Binding ListPainel_IsRefreshing}"
>
</ListView>
Part of ViewCell
protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
dynamic temp = BindingContext;
PainelDto painel = (PainelDto)temp;
...
if(painel.HasDetalhes)
{
Button detalhes = new Button()
{
Text="VER DETALHES",
FontSize = 12,
TextColor = Color.FromHex("#4482ff"),
HorizontalOptions = LayoutOptions.End,
VerticalOptions = LayoutOptions.Start,
HeightRequest = 20,
WidthRequest = 120,
BackgroundColor = Color.DeepPink
};
detalhes.SetBinding(
Button.CommandProperty
, new Binding(
"ViewDetalhesCommand"
, BindingMode.Default
, null
, null
, null
, _viewModelPainel
)
);
box.Children.Add(detalhes);
}
...
View = box;
}
Unselected Item
Selected Item
Some one know how to fix this?
It is the default behavior when selecting cell in iOS.
We need a custom renderer for ViewCell to disable the effect.
Create a Class called NativeiOSCellRenderer in iOS project.
Code :
[assembly: ExportRenderer(typeof(ViewCell), typeof(NativeiOSCellRenderer))]
namespace FormsListViewSample.iOS
{
class NativeiOSCellRenderer : ViewCellRenderer
{
public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
{
UITableViewCell cell = base.GetCell(item, reusableCell, tv);
cell.SelectionStyle = UITableViewCellSelectionStyle.None;
return cell;
}
}
}

Popup picker in Xamarin iOS

I need to implement a picker like in this image. How can I do this? I'm using Xamarin.iOS but I can use an answer with native code and translate it myself.
use an ActionSheet
actionSheetButton.TouchUpInside += ((sender, e) => {
// Create a new Alert Controller
UIAlertController actionSheetAlert = UIAlertController.Create("Action Sheet", "Select an item from below", UIAlertControllerStyle.ActionSheet);
// Add Actions
actionSheetAlert.AddAction(UIAlertAction.Create("Item One",UIAlertActionStyle.Default, (action) => Console.WriteLine ("Item One pressed.")));
actionSheetAlert.AddAction(UIAlertAction.Create("Item Two",UIAlertActionStyle.Default, (action) => Console.WriteLine ("Item Two pressed.")));
actionSheetAlert.AddAction(UIAlertAction.Create("Item Three",UIAlertActionStyle.Default, (action) => Console.WriteLine ("Item Three pressed.")));
actionSheetAlert.AddAction(UIAlertAction.Create("Cancel",UIAlertActionStyle.Cancel, (action) => Console.WriteLine ("Cancel button pressed.")));
// Required for iPad - You must specify a source for the Action Sheet since it is
// displayed as a popover
UIPopoverPresentationController presentationPopover = actionSheetAlert.PopoverPresentationController;
if (presentationPopover!=null) {
presentationPopover.SourceView = this.View;
presentationPopover.PermittedArrowDirections = UIPopoverArrowDirection.Up;
}
// Display the alert
this.PresentViewController(actionSheetAlert,true,null);
});
You can assign a PickerView to TextField's InputView property and assign a Toolbar to InputAccessoryView, like this:
UIToolbar toolBar = new UIToolbar(new CGRect(0, 0, 320, 44));
UIBarButtonItem flexibleSpaceLeft = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace,null,null);
UIBarButtonItem doneButton = new UIBarButtonItem("OK",UIBarButtonItemStyle.Done,this, new ObjCRuntime.Selector("DoneAction"));
UIBarButtonItem[] list = new UIBarButtonItem[] { flexibleSpaceLeft, doneButton };
toolBar.SetItems(list, false);
UIPickerView pickerView = new UIPickerView(new CGRect(0, 44, 320, 216));
pickerView.DataSource = new MyUIPickerViewDataSource();
pickerView.Delegate = new MyUIPickerViewDelegate();
pickerView.ShowSelectionIndicator = true;
textField.InputAccessoryView = toolBar;
textField.InputView = pickerView;
After you implement your DataSource and Delegate of the pickerview, it can work like this:
Thanks Kevin Li for the answer, just wanted to give a simplified version in case anyone needs this. Not exactly what was originally asked for, but does the job.
var pickerView = new UIPickerView();
var sports = new string[] { "baseball", "basketball", "wiener dog" };
var model = new PickerModel(sports);
model.PickerChanged += (sender, e) =>
{
txtSport.Text = sports[e.SelectedValue];
};
pickerView.Model = model;
pickerView.ShowSelectionIndicator = true;
// setting picker view to your textfield
txtSport.InputView = pickerView;

Xamarin FlyoutNavigationController does not show section FooterView

I want to implement a flyout navigation in Xamarin.iOS. For this purpose I am using the FlyoutNavigationController, which works good so far.
But now I have to display some additional content underneath the navigation list inside of the flyout-menu (basically an image, some labels and buttons). For this purpose, I wanted to use the FooterView property of the "section" control that holds the menu items.
When I set
section.Footer = "Test";
it will work (I can see the text), but when I use the 'FooterView' property, nothing shows up:
public class ViewController : UIViewController
{
FlyoutNavigationController navigation;
private const string PageNamePage1 = "The first page";
private const string PageNamePage2 = "The second page";
readonly string[] pages =
{
PageNamePage1,
PageNamePage2
};
public override void ViewDidLoad()
{
base.ViewDidLoad();
navigation = new FlyoutNavigationController
{
View =
{
Frame = UIScreen.MainScreen.Bounds
}
};
View.AddSubview(navigation.View);
var elements = pages.Select(page => new StringElement(page) as Element);
navigation.NavigationRoot = new RootElement("Induserv App");
var section = new Section();
section.AddAll(elements);
var uiTextView = new UITextView {Text = "--> This won't show!"};
section.FooterView = new UIView
{
uiTextView
};
navigation.NavigationRoot.Add(section);
navigation.ViewControllers = new UIViewController[]
{
new UINavigationController(new Page1Controller(navigation, PageNamePage1)),
new UINavigationController(new Page2Controller(navigation, PageNamePage2)),
};
}
}
Does anyone have an idea what it needs to display this footer here? Is there another way to put some controls in this panel?
Set a frame for both the footer view and uiTextView, like so:
var uiTextView = new UITextView(new RectangleF(0, 0, 10, 10)) {Text = "--> This won't show!"};
section.FooterView = new UIView(new RectangleF(0, 0, 10, 10))
{
uiTextView
};

Resources