Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

UIBarButtonItem Refresh

$
0
0

I have a UIBarButtonItem that is behaving inexplicably in a Xamarin Forms app targeting iOS and Android.

The goal is to have a button on the top right of the nav. The button is a shopping cart and a badge number tells you how many items you have in the cart. Everything works super on Android but on iOS I cannot get the UIBarButtonItem to update in some cases.

The app has two levels of nav for products - a category page and and items page. If you go to the items page and add items to the cart, it updates fine right when the count property changes. However, when you navigate back to the category page it will not update. I've traced it very closely and I can see the code being called, I can see the right number being set, I have error handling and can see that no errors are raised but the button does not update.

The following custom renderer is called in the OnAppears method.

`public async void AddRightButton(UIViewController viewController, string iconPath, System.Windows.Input.ICommand command, int badge)
{

        try
        { 
            var view = new IconWithBadge(await IOSResourceHelper.GetImageFromImageSource(ImageSource.FromFile(iconPath)), badge);

            var customRightItem = new UIBarButtonItem(view);

             // tried - viewController.NavigationItem.SetRightBarButtonItem(customRightItem, true);
             // tried resetting it first - viewController.NavigationItem.SetRightBarButtonItem(null, true);

           /* tried

                viewController.NavigationItem.RightBarButtonItems = new UIBarButtonItem[]
                {
                    customRightItem
                };
                */

       viewController.NavigationItem.RightBarButtonItem = customRightItem;
            }
        }
        catch (Exception exc)
        {
            Debug.WriteLine("CustomNavigationPageRenderer(iOS).AddRightButton - " + exc.Message);
            Debugger.Break();
        }

}

public IconWithBadge(UIImage image,int badgeValue):base(image)
{
try
{
int width = 14;
int height = 14;
string badgeString = badgeValue.ToString();

            var textLayer = new CATextLayer();
            textLayer.Frame = new CGRect(Layer.Frame.Width  - (width / 2), - height / 2, width, height);

            textLayer.CornerRadius=5;
            textLayer.String=badgeString;
            textLayer.FontSize = 12;
            textLayer.SetFont(CGFont.CreateWithFontName("Helvetica Neue Light"));


            Color bgColor=ResourceHelper.GetResource<Color>("colorBrightRed");
            var bgCGColor= new CGColor((nfloat)bgColor.R, (nfloat)bgColor.G, (nfloat)bgColor.B);
            textLayer.BackgroundColor=bgCGColor;
            textLayer.AlignmentMode="center";
            Color foreColor=ResourceHelper.GetResource<Color>("colorWhite");
            var foreCGColor= new CGColor((nfloat)foreColor.R, (nfloat)foreColor.G, (nfloat)foreColor.B);
            textLayer.ForegroundColor = foreCGColor;
            Layer.AddSublayer(textLayer);
        }
        catch(Exception exc)
        {
            Debug.WriteLine("CustomControls.IconWithBadge - " + exc.Message);
            Debugger.Break();
        }
    } `

Again, this code works on the child page and everything works on the Android app. It seems like it has to be something specific to the iOS custom renderer related to coming back to the page - even though the code is fine iOS/Xam refuses to make the update.

I'd be grateful for any insights.

Thanks,
Michael


Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>