Forum Xamarin.iOS

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

How to change the title bar color of the window.

Hello,
I'm a beginner on Xamarin and I can't translate Objective-c to C#. The way I use to change the color of the title bar on Xcode is by creating a new class:

class MainWindow: NSWindowController {
    override func windowDidLoad() {
        super.windowDidLoad()
        super.window?.backgroundColor = NSColor(calibratedRed: 105/255, green: 105/255, blue: 105/255, alpha: 1.0)
    }
}

and then assigning the main Window Controller class to this custom one.

I translated this class to C# like this:

public partial class MainWindowColored: NSWindowController {
    public override void WindowDidLoad()
    {
        base.WindowDidLoad();
        base.Window.BackgroundColor = NSColor.FromRgb(red: 105 / 255f, green: 105 / 255f, blue: 105 / 255f);
    } 
}

Also I added on the ViewDidLoad()
this.View.Superview.WantsLayer = true;
and I tried to assign it the same way. Then I get the following message:

I click ok and then I get the next message:

And of course, it doesn't work.
Any recommendations?

Best Answer

  • FotiosTragopoulosFotiosTragopoulos USMember ✭✭
    Accepted Answer

    Thanks everyone for the help. I actually found the solution.

    In order to change the Title Bar on an OS X application there is a need for a new class as I mentioned before (Sorry for not providing the whole code @ChrisHamons as you mentioned)

    using System;
    using Foundation;
    using AppKit;
    
    namespace AtERP
    {
        public partial class TitleBarDesignerController : NSWindowController
        {
            public TitleBarDesignerController(IntPtr handle) : base(handle)
            {
            }
    
            [Export("initWithCoder:")]
            public TitleBarDesignerController(NSCoder coder) : base(coder)
            {
            }
    
            public TitleBarDesignerController() : base("TitleBarDesigner")
            {
            }
    
            public override void AwakeFromNib()
            {
    
                base.AwakeFromNib();
                base.WindowDidLoad();
                base.Window.BackgroundColor = NSColor.FromRgb(red: 125 / 255f, green: 125 / 255f, blue: 125 / 255f);
            }
        }
    }
    

    Then you assign this class to the Window Controller, as shown in the next picture

    And that's all.
    Don't add any other code on the ViewDidLoad() that refers to this view.
    Sorry @TimothyRisi, the point is to change the color not ot make it dissapear.

Answers

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    So as the first pop up suggests, your MainWindowColored does not have a [Register] attribute or somehow your project is a inconsistent state.

    Did you create your view from New File -> Mac -> View or by hand? There is a some basic decoration needed for NSObject derived classes, specially ones instanced up by obj-c.

    [Foundation.Register ("View")] // Register our class with obj-c
    public partial class View : NSView
    {
        public View (IntPtr handle) : base (handle) // Called when obj-c creates an instance of us in obj-c
        {
        }
    }
    

    Unfortunately without a project sample showing your issue, the best people here can do is guess.

  • FotiosTragopoulosFotiosTragopoulos USMember ✭✭
    edited August 2017

    The following window is what I have:

    And the next window is what I want to do:

    There is a different color in the Title Bar that I applied by creating a Class that inherits from the NSWindowController Class in Swift 3.0 and connected it to the Window Controller. Is there any way to get the result of the second picture with Visual Studio for Mac?
    Thank you very much.

  • TimothyRisiXMTimothyRisiXM USXamarin Team Xamurai
    edited August 2017

    Many simple questions like this have answers already on Stack Overflow:

    Searching for "cocoa change title bar color" for example.

    Those answers will likely be in objective-c, but this documentation will help convert it to c#:

    https://developer.xamarin.com/guides/mac/application_fundamentals/patterns/
    https://developer.xamarin.com/guides/mac/application_fundamentals/mac-apis/

    From your last post, it appears that you want the status bar to have the same grey background as the rest of the window?

    If that is the case, based on this post on SO, you should simply be able to set the window's TitlebarAppearsTransparent to true and set the windows BackgroundColor to the desired color.

  • FotiosTragopoulosFotiosTragopoulos USMember ✭✭
    Accepted Answer

    Thanks everyone for the help. I actually found the solution.

    In order to change the Title Bar on an OS X application there is a need for a new class as I mentioned before (Sorry for not providing the whole code @ChrisHamons as you mentioned)

    using System;
    using Foundation;
    using AppKit;
    
    namespace AtERP
    {
        public partial class TitleBarDesignerController : NSWindowController
        {
            public TitleBarDesignerController(IntPtr handle) : base(handle)
            {
            }
    
            [Export("initWithCoder:")]
            public TitleBarDesignerController(NSCoder coder) : base(coder)
            {
            }
    
            public TitleBarDesignerController() : base("TitleBarDesigner")
            {
            }
    
            public override void AwakeFromNib()
            {
    
                base.AwakeFromNib();
                base.WindowDidLoad();
                base.Window.BackgroundColor = NSColor.FromRgb(red: 125 / 255f, green: 125 / 255f, blue: 125 / 255f);
            }
        }
    }
    

    Then you assign this class to the Window Controller, as shown in the next picture

    And that's all.
    Don't add any other code on the ViewDidLoad() that refers to this view.
    Sorry @TimothyRisi, the point is to change the color not ot make it dissapear.

Sign In or Register to comment.