Xamarin IOS hide bar back button - c#

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);

Related

XAMARIN: How to Link [Android] Hardware Back Button to Webkit.WebView?

I'm trying to link the hardware back button into my WebViews in Xamarin for Android. My WebViews are contained within OnCreate instances of TabHost (which is deprecated, but I'm using it anyway) I've got this inside my MainActivity : TabActivity class
public override void OnBackPressed()
{
base.OnBackPressed();
}
and here's an example of one of my Tab Activity Classes
[Activity]
public class SpeakersActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
//set the content view
SetContentView(Resource.Layout.subs);
//declare webview and tell our code where to find the XAML resource
WebView subWebView = FindViewById<WebView>(Resource.Id.webViewSubs);
//set the webview client
subWebView.SetWebViewClient(new WebViewClient());
//load the subscription url
subWebView.LoadUrl("https://www.bitchute.com/subscriptions/");
//enable javascript in our webview
subWebView.Settings.JavaScriptEnabled = true;
//zoom control on? This should perhaps be disabled for consistency?
//we will leave it on for now
subWebView.Settings.BuiltInZoomControls = true;
subWebView.Settings.SetSupportZoom(true);
//scrollbarsdisabled
// subWebView.ScrollBarStyle = ScrollbarStyles.OutsideOverlay;
subWebView.ScrollbarFadingEnabled = false;
}
}
and I've seen a lot information how to use this
subWebView.GoBack();
to goback in a webview, but the problem is that my WebViews are not within the scope of my hardware back button. The hardware back button is inside mainactivity class and my webviews are inside individual instances of the tab activities.
What's the best way to correct this issue? Thank you!
Thanks so much #SushiHangover !!!
I solved it like this:
[Activity]
public class SpeakersActivity : Activity
{
public override void OnBackPressed()
{
WebView subWebView = FindViewById<WebView>(Resource.Id.webViewSubs);
subWebView.GoBack();
}
}
EDIT: the way you do this with a ViewPager and Fragment is as follows:
//put this code in your MainActivity.cs
public override bool OnKeyDown(Android.Views.Keycode keyCode, KeyEvent e)
{
if (e.KeyCode == Android.Views.Keycode.Back)
{
switch (_viewPager.CurrentItem)
{
case 0:
_fm1.WebViewGoBack();
break;
case 1:
_fm2.WebViewGoBack();
break;
case 2:
_fm3.WebViewGoBack();
break;
case 3:
_fm4.WebViewGoBack();
break;
case 4:
_fm5.WebViewGoBack();
break;
}
}
return false;
}
then in a fragment instantiate WebView assign in OnCreate and create a method for going back inside Fragment :
protected static WebView _wv;
protected static View _view;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
_view = inflater.Inflate(Resource.Layout.TheFragmentLayout1, container, false);
_wv = _view.FindViewById<WebView>(Resource.Id.webView1);
//lots of code
}
public void WebViewGoBack()
{
if (_wv.CanGoBack())
_wv.GoBack();
}

how to change navigation bar title icon on xamarin.forms ios?

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/

Navigate back to calling Fragment from another activity

I have a MainActivity which has 3 fragments. 2 of these fragments have lists in them and on click of a list item I go to another activity (ie. NewActivity). I have an ActionBar implemented on the NewActivity and want to go back to the calling fragment from this activity.
I have the following code:
MainActivity.cs
SupportActionBar.SetDisplayHomeAsUpEnabled(true);
SupportActionBar.SetDisplayShowHomeEnabled(true);
var ft = SupportFragmentManager.BeginTransaction();
ft.Add(Resource.Id.fragment_container, homeFragment, "FRAG_HOME").SetBreadCrumbShortTitle("FRAG_HOME");
ft.Add(Resource.Id.fragment_container, FragmentList1, "FRAG_LIST1").SetBreadCrumbShortTitle("FRAG_LIST1");
ft.Hide(FragmentList1);
ft.Add(Resource.Id.fragment_container, FragmentList2, "FRAG_LIST2").SetBreadCrumbShortTitle("FRAG_LIST2");
ft.AddToBackStack(null);
ft.Hide(FragmentList2);
ft.Commit();
FragmentList1.cs
void LvLatestArticles_ItemClick (object sender, AdapterView.ItemClickEventArgs e)
{
Intent intent = new Intent(Activity, typeof(NewActivity));
StartActivity(intent);
}
NewActivity.cs
public override bool OnOptionsItemSelected(IMenuItem item)
{
switch(item.ItemId)
{
case Android.Resource.Id.Home:
SupportFragmentManager.PopBackStackImmediate();
break;
}
return base.OnOptionsItemSelected(item);
}
In the above code, I override the Home button functionality on ActionBar. But it simply does not do anything. How do I go forward with this? Any help would be appreciated
Try calling OnBackPressed() on the Home button. Something like so :-
public override bool OnOptionsItemSelected(IMenuItem item){
switch(item.ItemId){
case Android.Resource.Id.Home:
base.OnBackPressed();
break;
}
return base.OnOptionsItemSelected(item);
}

Xamarin DialogViewController not disposed when Root is set

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);
}
}

How to enable/add a BackButton on the NavigationController?

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);

Categories

Resources