Setting NSView.Bounds have no effect

AlvaroRivoirAlvaroRivoir USMember ✭✭✭

I'm trying to set NSView.Bounds, but after performing it, the property still have CGRect(0,0,0,0) and the view it is not displayed at all. Is it a bug or I'm doing something wrong?

Tagged:

Posts

  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai
    edited December 2017

    @AlvaroRivoir said:
    I'm trying to set NSView.Bounds, but after performing it, the property still have CGRect(0,0,0,0) and the view it is not displayed at all. Is it a bug or I'm doing something wrong?

    Without more info it is impossible to say whether you are dong anything wrong. Where are you setting the NSView.Bounds? Are you literally using an NSView? In general you should not use NSView directly (https://developer.apple.com/documentation/appkit/nsview) but instead should use a framework subclass of NSView (like NSTextView) or your own subclass of NSView. That said, you may want to set the Frame rather than the Bounds. I just did a quick test and setting the Bounds would let me resize the NSView (created in storyboard), but not move it. Setting the Frame allowed me to move it as well.

    If the above does not help, can you provide a code sample? Make sure that there is context enough to show the method in which you are setting the NSView.Bounds and the class that the method is in (with any base class shown as well). Also whether you are modifying an NSView that is in a xib/storyboard or whether this is an NSView that you are creating in code would be helpful to know. In fact, best would be if you could provide a buildable and runnable test project that showed the issue to guarantee that all necessary context is provided.

  • AlvaroRivoirAlvaroRivoir USMember ✭✭✭
    edited January 2018

    Here the steps to reproduce it

    1. Create a Mac Cocoa App
    2. In ViewController.cs write the following code

      public override void ViewDidLoad()
      {
                  base.ViewDidLoad();
      
                  // Do any additional setup after loading the view.
                  var view = new NSView();
                  view.WantsLayer = true;
                  view.Layer.BackgroundColor = NSColor.Red.CGColor;
                  view.Bounds = new CoreGraphics.CGRect(0, 0, 100, 100);
                  //view.Frame = new CoreGraphics.CGRect(0, 0, 100, 100);
                  View.AddSubview(view);
      }
      

    The view is not displayed despite the bounds were set, and if you ask the Bounds property after setting it it returns all zeros.

    If you uncomment the line which sets the Frame it works and it actually set the Bounds too.

  • AlvaroRivoirAlvaroRivoir USMember ✭✭✭

    in iOS the same code(replace NS by UI) works ok. When you set the Bounds, you can ask the property and it gives you the value you just set, and the Frame reflects the value set in Bounds and vice versa.

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    I believe this is the code you are trying to write:

        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();
    
            // Do any additional setup after loading the view.
            var view = new NSView (new CoreGraphics.CGRect (0, 0, 100, 100));
            view.WantsLayer = true;
            view.Layer.BackgroundColor = NSColor.Red.CGColor;
            View.AddSubview (view);
        }
    

    I don't believe you are hitting a bug, its just that Cocoa can be really strange when it comes to Frame/Bounds on views that are not in rooted in the visual tree.

    On a semi-unrelated note, if you haven't read through the documentation listed here you should consider doing so, it can often be helpful as Cocoa does not do what you'd often expect and Xamarin.Mac just exposes this behavior directly to C#.

Sign In or Register to comment.