I have tried many ways, but haven't succeeded.
In this way I can create a new UIBarButtonItem and it works, the problem is that it dosent lock like a backButton/ArrowBackButton:
public override void ViewWillAppear (bool animated)
{
base.ViewWillAppear (animated);
this.NavigationItem.LeftBarButtonItem = new UIBarButtonItem ("Tillbaka", UIBarButtonItemStyle.Plain, delegate(object sender, EventArgs e) {
this.NavigationController.PopViewControllerAnimated (true);
});
}
Have tried this, but haven't worked:
public override void ViewWillAppear (bool animated)
{
base.ViewWillAppear (animated);
this.NavigationItem.SetHidesBackButton(false,true);
}
With MonoTouch.Dialog you have to set a "pushing" flag in order for it to show the back button. You can do this in the constructor, as indicated below:
public class MyViewController : DialogViewController
{
public MyViewController
: base(new RootElement("foo"), true)
{
}
}
Can you give some context to this please? How is this ViewController being added to the NavigationController ?
If you use the PushViewController method (as below) then a back button will be automatically added.
var viewController = new UIViewController();
this.NavigationController.PushViewController(viewController, true);
Related
i want to change navigation bar title icon on my xamarin.forms ios application.You can find requested point at the attached image.app_screenshot
You can achieve this by using Custom Renderers on Xamarin.Forms. Since each UIViewController has its own NavigationItem, you should do this in your particular page which you want to modify its LeftBarButtonItem. Here is my Renderer for you referring to:
[assembly: ExportRenderer(typeof(CustomPage), typeof(CustomPageRenderer))]
namespace UIBarButtomItemsForms.iOS
{
public class CustomPageRenderer : PageRenderer
{
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
var navigationItem = NavigationController.TopViewController.NavigationItem;
UIBarButtonItem leftItem = new UIBarButtonItem(UIImage.FromBundle("Image.png"), UIBarButtonItemStyle.Plain, (sender, args) =>
{
});
navigationItem.SetLeftBarButtonItem(leftItem, false);
}
}
}
Then on Forms you can use this CustomPage, please notice that you should make your MainPage wrapped in a NavigationPage: MainPage = new NavigationPage(new MainPage());
Take a look at Change the Back Button: https://developer.xamarin.com/recipes/ios/content_controls/navigation_controller/change_the_back_button/
I am new to iOS, and I am using Xamarin Studio on MacOS (I already have my Xamarin.Droid app and now making it on iOS).
I am struggling using an UISearchBar, I want a really basic usage tho .. I tried to follow this recipes (https://developer.xamarin.com/recipes/ios/content_controls/search-controller/) but still can't manage to make it work.
What I am trying to get is a SearchBar, in which the user should type text then press the "search" button on the keyboard. Pressing the search button should call code that will populate an UITableView with data retrieved on my webservice.
Here's my Storyboard structure, a ViewController which contains an UITableView with a name
I tried the following code, everything seems to work fine, but when I press search on the keyboard, SearchButtonClicked event is never fired ... And I can't figure out why
public partial class SearchViewController : UIViewController
{
public SearchViewController(IntPtr handle) : base(handle)
{
}
public override void ViewDidLoad()
{
var searchController = new UISearchController(this);
searchController.SearchBar.SizeToFit();
searchController.SearchBar.ShowsCancelButton = true;
searchController.SearchBar.SearchButtonClicked += delegate {
Console.WriteLine("Hello ????");
this.Title = "This code is never called";
};
RunnersSearchTableView.TableHeaderView = searchController.SearchBar;
base.ViewDidLoad();
}
}
Also tried
public partial class SearchViewController : UIViewController
{
public SearchViewController(IntPtr handle) : base(handle)
{
}
public override void ViewDidLoad()
{
var searchController = new UISearchController(this);
searchController.SearchBar.SizeToFit();
searchController.HidesNavigationBarDuringPresentation = true;
searchController.SearchBar.ShowsCancelButton = true;
searchController.SearchBar.Delegate = new MySearchBarDelegate();
RunnersSearchTableView.TableHeaderView = searchController.SearchBar;
base.ViewDidLoad();
}
}
public class MySearchBarDelegate : UISearchBarDelegate
{
public MySearchBarDelegate()
{
}
public override void SearchButtonClicked(UISearchBar searchBar)
{
Console.WriteLine("TEST");
searchBar.ResignFirstResponder();
base.SearchButtonClicked(searchBar);
}
}
And also tried
public partial class SearchViewController : UIViewController
{
public SearchViewController(IntPtr handle) : base(handle)
{
}
public override void ViewDidLoad()
{
var searchController = new UISearchController(this);
searchController.SearchBar.SizeToFit();
searchController.HidesNavigationBarDuringPresentation = true;
searchController.SearchBar.ShowsCancelButton = true;
searchController.SearchBar.WeakDelegate = this;
RunnersSearchTableView.TableHeaderView = searchController.SearchBar;
base.ViewDidLoad();
}
[Export("searchBarSearchButtonClicked:")]
public virtual void SearchButtonClicked(UISearchBar searchBar)
{
Console.WriteLine("hello??");
searchBar.ResignFirstResponder();
}
}
None of them seems to work
You need to register delegate for searchbar .
[Foundation.Register("UISearchBar", true)]
for more : https://developer.xamarin.com/api/type/UIKit.UISearchBar/
I want to display a toast Message. If I'd do this in onCreate() it'd work fine. But I want to do it like this and I get an error:
Java.Lang.NullPointerException: Attempt to invoke virtual method
'android.content.res.Resources android.content.Context.getResources()'
on a null object reference
What should I do?
public void textToast(string textToDisplay) {
Toast.MakeText(this,
textToDisplay, ToastLength.Long).Show();
}
class SampleTabFragment : Fragment
{
Button add;
MainActivity main = new MainActivity();
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
var view = inflater.Inflate(Resource.Layout.Tab, container, false);
add = view.FindViewById<Button>(Resource.Id.add);
add.Click += Click;
return view;
}
void Click(object sender, EventArgs eventArgs)
{
main.textToast( "I like Toast!");
}
}
The Java.Lang.NullPointerException is triggered because you are manually creating and using an instance of MainActivity.
Instead of using a custom instance of MainActivity to display your toast message in Click, simplify your code to use the fragments existing activity reference:
public void textToast(string textToDisplay) {
Toast.MakeText(this,
textToDisplay, ToastLength.Long).Show();
}
class SampleTabFragment : Fragment
{
Button add;
// Remove manual creation code
// MainActivity main = new MainActivity();
// ...
void Click(object sender, EventArgs eventArgs)
{
(Activity as MainActivity).textToast( "I like Toast!");
}
}
This code assumes that the owning activity is always an instance of MainActivity.
See:
Fragment getActivity()
How to use the method of an Activity in a DialogFragment?
If I understand correctly your question, I think a good solution may be this one:
public void makeToast(Context ctx, string str)
{
Toast.MakeText(ctx, str, ToastLength.Long).Show();
}
And when you use it in every fragment you have, you can call it just writing:
makeToast(this.Activity, "test!");
Works for me, let me know :)
I'm having a weird problem. I'm using Subclasses of DialogViewController for a multi-page interview. Each page presents a number of fields that can be edited, and hitting save in the upper right pushes the next page onto the NavigationController. It seemed to be working fine, but it became apparent that backing out and repeating the interview leaks memory. I created a simple test case that properly cleans up the button event for the navigation item. In this example, Dispose is called when the second controller is popped, but only if I don't set Root to something other than null (i.e. this works as expected if I comment the Root = ... line). Here's the code. Please tell me I'm missing something stupid.
public class TestViewController : DialogViewController { int mPage;
public TestViewController (int page) : base (null, true)
{
mPage = page;
Root = new RootElement ("Testing") {
new Section () {
new StringElement ("Page: " + mPage)
}
};
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
NavigationItem.RightBarButtonItem = new UIBarButtonItem (UIBarButtonSystemItem.Play);
}
public override void ViewWillAppear (bool animated)
{
base.ViewWillAppear (animated);
NavigationItem.RightBarButtonItem.Clicked += OnClicked;
}
public override void ViewDidDisappear (bool animated)
{
base.ViewDidDisappear (animated);
NavigationItem.RightBarButtonItem.Clicked -= OnClicked;
}
private void OnClicked(object sender, EventArgs e)
{
NavigationController.PushViewController(new TestViewController(mPage + 1), true);
}
protected override void Dispose (bool disposing)
{
base.Dispose (disposing);
}
}
I am trying to hide the back button from my navigationcontroller on a certain view(using storyboard)
I tried to hide the bar back button overriding the ViewWillAppear, but it does not seem to happen.
Here is the code:
public override void ViewWillAppear (bool animated)
{
base.ViewWillAppear (animated);
this.NavigationController.NavigationItem.SetHidesBackButton (true, true);
}
just change to :
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
this.NavigationItem.SetHidesBackButton (true, false);
}
Below is the best answer which works well with xamarin forms too
Add this code in App delegate.This solved my problem too
UIBarButtonItem.Appearance.SetBackButtonTitlePositionAdjustment (new UIOffset (-100, -60), UIBarMetrics.Default);