Stephan
Stephan

Reputation: 1999

How to set ShadowImage for navigation bar in Xamarin iOS

In a Xamarin iOS (not Forms) app I want to add a segmented control to the navigation bar so I followed some tutorials on this. It basically works but the only thing that's missing is to remove the 1px hair line below the navigation bar.

What I have to remove the hair line currently is:

public partial class MainViewController : UIViewController
{
    public MainViewController (IntPtr handle) : base (handle)
    {

    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        this.NavigationController?.NavigationBar.SetBackgroundImage(new UIImage(), UIBarMetrics.Default);
        this.NavigationController?.NavigationBar.ShadowImage = new UIImage();
    }
}

However, the line where I set an empty shadow image throws the following error:

Error: The left-hand side of an assignment must be a variable, property, or indexer.

But to my best knowledge, ShadowImage is a property.

Am I wrong? How can I set ShadowImage so that the hair line is removed?

Upvotes: 0

Views: 426

Answers (2)

ColeX
ColeX

Reputation: 14475

Refer to ? Operator C# , it is used for null checking.

When you assign value to ShadowImage ,

this.NavigationController?.NavigationBar.ShadowImage

equals to

if(this.NavigationController != null ){
    this.NavigationController.NavigationBar.ShadowImage
}

It is not variable, property, or indexer.

So , Modify your code as below

if(this.NavigationController != null ){
    this.NavigationController.NavigationBar.SetBackgroundImage(new UIImage(), UIBarMetrics.Default);
    this.NavigationController.NavigationBar.ShadowImage = new UIImage();
}

Upvotes: 0

Ashray P. Shetty
Ashray P. Shetty

Reputation: 700

Please refer this blog : https://xamgirl.com/navigation-bar-with-shadow-in-xamarin-forms/

The author of the blog has explained very clearly how to achieve this.

Hope this helps!!!

Upvotes: -1

Related Questions