iOS gradient NSArray problem

Hello

There is any issue with NSArray and WithObjects. I’m trying to set gradient for button. This code do not compile because first NSArray parameter should be id - compiler says. Casting it to id give error that it cannot be casted as id. Closed circle. In Objective-c all params for gradient are casted as id.

CAGradientLayer gradientLayer = CAGradientLayer.layer();
gradientLayer.frame = butt.layer.bounds;
gradientLayer.colors = new NSArray WithObjects(UIColor.blackColor.CGColor, UIColor.whiteColor.CGColor, null);
gradientLayer.locations = NSArray.arrayWithObjects(NSNumber.numberWithFloat(0), NSNumber.numberWithFloat(1), null);
gradientLayer.cornerRadius = butt.layer.cornerRadius;
butt.layer.addSublayer(gradientLayer);

This one works, but first parameter is ignored in gradient (must be CGColorRef):

gradientLayer.colors = new NSArray WithObjects(UIColor.blackColor, UIColor.whiteColor.CGColor, null);

How to resolve, what I’m doing wrong?

Original Objective-C code (colors are different). Please use oxidizer and try to compile:

gradientLayer.colors = [NSArray arrayWithObjects:
(id)[UIColor colorWithWhite:1.0f alpha:0.1f].CGColor,
(id)[UIColor colorWithWhite:0.4f alpha:0.5f].CGColor,
nil];

Aha, QuartzCore reference must be added.

[EDIT[

Temporary workaround is to define first object and location as UIColor (blank - which is bad as color value for gradient), second param will be first color and add location for second (really first) same as first.

gradientLayer.colors = new NSArray WithObjects(UIColor.blackColor, UIColor.blackColor.CGColor, UIColor.whiteColor.CGColor, null);
gradientLayer.locations = NSArray.arrayWithObjects(NSNumber.numberWithFloat(0), NSNumber.numberWithFloat(0), NSNumber.numberWithFloat(1), null);

b.r.

Thanks, logged as bugs://72706

bugs://72706 got closed with status fixed.

Just small info. Got beta access and first testing fire and there this bug exists too. I know is not listed in changelog (not fixed), but just info that fire is affected.
Good work!

b.r.

Hmm checked beta 8.2.88.1811 in VS and is not fixed. Still cannot compile:

gradientLayer.colors = new NSArray WithObjects(UIColor.blackColor.CGColor, UIColorFromRGB(0xFF00FF00).CGColor, null);

First parameter must be as CGColor.

Error code:

Error 3 (E486) Parameter 1 is “CGColorRef”, should be “id”, in call to instancetype NSArray.initWithObjects(id firstObj, params dynamic[] param1)

b.r.

bump, maybe my response was not noticed, but bug still exist :smile:

b.r.

My apologies. I’ll personally have look tomorrow (i’m traveling today)

bugs://72706 got reopened.

np. take Your time :slight_smile: 'm just tracking progress :slight_smile:

Refreshing with question if issue was checked? :smile:

b.r.

Try this:
new NSArray WithObjects(bridge(UIColor.blackColor.CGColor), bridge(UIColor.greenColor.CGColor), null);

bugs://72706 got closed with status fixed.

I got error message while compile:

Error 1 (E0) Internal error: Object reference not set to an instance of an object

ps. Too fast with fixing :slight_smile: Is not fixed :smile:

b.r.

Got a full fragment I can try? I tried what I pasted.

try this please:

[IBOutlet]
    public UIButton butt { get; set; }
     
    public override void viewDidLoad()
    {
    	base.viewDidLoad();
     
    	CAGradientLayer gradientLayer = CAGradientLayer.layer();
    	gradientLayer.frame = butt.layer.bounds;
    	gradientLayer.colors = new NSArray WithObjects(bridge(UIColor.blackColor.CGColor), bridge(UIColor.greenColor.CGColor), null);
    	gradientLayer.locations = NSArray.arrayWithObjects(NSNumber.numberWithFloat(0), NSNumber.numberWithFloat(1), null);
    	gradientLayer.cornerRadius = butt.layer.cornerRadius;
    	butt.layer.addSublayer(gradientLayer);
    }

b.r.

Ah! My paste lost <id>, try this (next version will properly error)


			  	CAGradientLayer gradientLayer = CAGradientLayer.layer();
    	gradientLayer.frame = butt.layer.bounds;
    	gradientLayer.colors = new NSArray WithObjects(bridge<id>(UIColor.blackColor.CGColor), bridge<id>(UIColor.greenColor.CGColor), null);
    	gradientLayer.locations = NSArray.arrayWithObjects(NSNumber.numberWithFloat(0), NSNumber.numberWithFloat(1), null);
    	gradientLayer.cornerRadius = butt.layer.cornerRadius;
    	butt.layer.addSublayer(gradientLayer);

it’s working now ! :slight_smile: Thank You :smile:
So it will be like this, I mean always need to use bridge or it will be changed in future?

b.r.

Yes always bridge; I think objc requiers it too when ARC is enabled.

Okay. Just read again about bridge and was mentioned that can be used to cast to id. Thanks!

b.r.