How to position UIColorwell accurately programmatically?

211 Views Asked by At

I'm not using interface builder/storyboard, I do everything programmatically and without auto-layout. In that context, if you setup a UIColorwell and give it a frame, the actual colorwell image (i.e. the rainbow circle) will always be the same size and be stuck to the top left of its own frame, regardless of the frame size. I was expecting it to scale along with its frame size. This fixed size, which is different across devices, makes it very difficult to line it up with any other images/SF symbols on your UI.

I tried various techniques to centre the colorwell, such as changing its contentHorizontalAlignment and contentVerticalAlignment but it still remains stuck to the top left. But even if I manage to centre it within its own frame, I still don't know the rendered circle point size, so I can't position it besides other circles of the same size (as done in iOS's Markup). I looked into and changed various UIControl properties but also to no avail.

Two questions: is there any way to find out the rendered point size/pixel size of the rainbow circle at runtime for a device? And is there any way to have it (the rainbow circle) centred on its own frame (this is important for the touchable rect).

I use objective C (not sure if this behaviour exists on Swift), I'm on iOS 14, haven't tested iOS 15 yet. I set up as normal:

colorWell =  [[UIColorWell alloc] init];
[colorWell  addTarget:self action:@selector(colorWellPressed:) forControlEvents:UIControlEventTouchUpInside];
colorWell.supportsAlpha = NO;
colorWell.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);

I'd very much appreciate any thoughts on this issue. Cheers.

1

There are 1 best solutions below

0
Jon L. On

This works for me:

UIColorWell *cPicker=[[UIColorWell alloc] initWithFrame:CGRectMake(50,10,60,40)];
cPicker.title=@"Select Color";
cPicker.supportsAlpha=NO;
cPicker.selectedColor=[UIColor blackColor];
[cPicker addTarget:self action:@selector(cWellC:) forControlEvents:UIControlEventValueChanged];
[cPicker addTarget:self action:@selector(cWellT:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:cPicker];[cPicker release];