0

So I have a 1px width with 50px height of a .png file and I want to tile this horizontally in a UISegmentedButton, so I did:

[segmentedCtrl setTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"btn-gradient-brown.png"]]];

All I got is however a black color. Why is this and how to do this correctly? Here's the 1px that I have, if it helps to clarify my point

enter image description here

adit
  • 32,574
  • 72
  • 229
  • 373

1 Answers1

0

You can try with core graphics methods to tile, look at this code from this question,

CGSize imageViewSize = imageView.bounds.size;
UIGraphicsBeginImageContext(imageViewSize);
CGContextRef imageContext = UIGraphicsGetCurrentContext();
CGContextDrawTiledImage(imageContext, (CGRect){ CGPointZero, imageViewSize }, tileImage);
UIImage *finishedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

It creates a titled image. you can set image as the background to your UIcontrol.

EDIT: tintColor is base for creating default gradient of segment control. So setting brown gradient image as tint color gives you black gradient. To set gradient colors,

  • subclass uisegmentcontrol class and override drawRect method.
  • Use backgroundImage and set proper image insets.
Community
  • 1
  • 1
Vignesh
  • 10,205
  • 2
  • 35
  • 73