Button image transparent when added to view - ios

I'm making a custom view to display a simple star rating system in my app. Each star is a UIButton that has a specific star image-file set as its image. Everything works fine in storyboard but when I run the app on a device each star image is slightly transparent.
When I set the background color of the buttons themselves, they are 100% opaque yet the images are still slightly transparent. Any reason why that same button's image wouldn't be opaque? I just want the stars to always be opaque and not dependent on background.
I just joined SO so I don't have enough reputation to post images yet but here's a link to an image that might explain the issue a bit better: photo
Basic code for the custom UIView:
- (void)layoutSubviews
{
[super layoutSubviews];
self.opaque = YES;
[self drawStars];
}
- (void)drawStars {
float imagePadding = self.padding;
CGFloat starWidth = self.selectedStarImage.size.width;
for (NSInteger counter = 0; counter < self.totalStars; counter++)
{
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(counter * (imagePadding + starWidth), 0, starWidth, starWidth)];
button.opaque = YES;
if (counter >= self.rating) {
[button setImage:self.starImage forState:UIControlStateNormal];
}
else {
[button setImage:self.selectedStarImage forState:UIControlStateNormal];
}
[self addSubview:button];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
button.tag=counter;
}
}
- (void)updateStars
{
for (UIView *subview in self.subviews) {
[subview removeFromSuperview];
}
[self drawStars];
}
Any help would be much appreciated.

It could happen that the alpha of view on which you are adding the buttons as subviews could be lesser than one.

i think you have to first change the image of star because its background it transparent that is the problem and check the background color of button.
Or
you can create whole transparent image and set proper background color which you want.
These images are not proper. or check it by using another images.

Related

iOS: How to achieve the same highlight that a UIButton background image has?

I am trying to achieve the same highlight that a UIButton background image has, but for a UIButton with no background image. This is what it looks like when the UIButton with an image background has been highlighted.
However, I am unsuccessful achieving this when using a non background image button. This is the code I have so far for my custom button.
- (void)setHighlighted:(BOOL)highlighted
{
if (highlighted) {
self.backgroundColor = [self.backgroundColor colorWithAlphaComponent:0.9f];
self.titleLabel.alpha = 0.9f;
} else {
self.backgroundColor = [self.backgroundColor colorWithAlphaComponent:1.0f];
self.titleLabel.alpha = 1.0f;
}
[super setHighlighted:highlighted];
}
The main problem is it isn't dark enough. Is there a way I can change the background color and label color to resemble a UIButton with an image background that has been highlighted?
Edit: I know alpha makes it lighter, but this was recommend in a thread when I was researching.
Edit: I created this method
- (void)setHighlighted:(BOOL)highlighted
{
if (highlighted) {
if (!overlayView) {
overlayView = [[UIView alloc] initWithFrame:self.bounds];
overlayView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
}
[self addSubview:overlayView];
} else {
[overlayView removeFromSuperview];
}
[super setHighlighted:highlighted];
}
This seems to work well, but not sure if there is a better alternative.

Wrong barButton item tint for play/pause buttons built programatically

I have a UIToolbar with audio controls. Sometime around iOS 7 upgrade the color of the bar changed and iOS started shading my buttons differently. That introduced a bug where the play and pause buttons which I change programatically don't match the new look of the toolbar:
Toolbar starts out looking fine:
Now I pressed play so code inserted pause button but wrong color:
Now I pressed pause and code inserted play button, again with wrong color:
I want the play and pause buttons to have that same dark look as the other buttons. I assume I have to do something differently when building the replacement buttons. The raw icon images are all that lighter color, but iOS toolbar seems to be automatically recoloring to darker color from the storyboard, as seen in first screenshot.
Here's the code I use to build the buttons:
- (UIBarButtonItem *) makeBarButton: (NSString *) imageName action:(SEL)targetAction
{
UIImage* image = [UIImage imageNamed:imageName];
CGRect frame = CGRectMake(0 - 20, 0 - 20, image.size.width + 20, image.size.height + 20);
UIButton *button = [[UIButton alloc] initWithFrame:frame];
[button addTarget:self action:targetAction forControlEvents:UIControlEventTouchUpInside];
[button setImage:image forState:UIControlStateNormal];
[button setShowsTouchWhenHighlighted:YES];
return [[UIBarButtonItem alloc] initWithCustomView:button];
}
- (void) setAsPlaying:(BOOL)isPlaying
{
self.rootViewController.playing = isPlaying;
// we need to change which of play/pause buttons are showing, if the one to
// reverse current action isn't showing
if ((isPlaying && !self.pauseButton) || (!isPlaying && !self.playButton))
{
UIBarButtonItem *buttonToRemove = nil;
UIBarButtonItem *buttonToAdd = nil;
if (isPlaying)
{
buttonToRemove = self.playButton;
self.playButton = nil;
self.pauseButton = [self makeBarButton:#"icon_pause.png" action:#selector(pauseAudio:)];
buttonToAdd = self.pauseButton;
}
else
{
buttonToRemove = self.pauseButton;
self.pauseButton = nil;
self.playButton = [self makeBarButton:#"icon_play.png" action:#selector(playAudio:)];
buttonToAdd = self.playButton;
}
// Get the reference to the current toolbar buttons
NSMutableArray *toolbarButtons = [[self.toolbar items] mutableCopy];
// Remove a button from the toolbar and add the other one
if (buttonToRemove)
[toolbarButtons removeObject:buttonToRemove];
if (![toolbarButtons containsObject:buttonToAdd])
[toolbarButtons insertObject:buttonToAdd atIndex:4];
[self.toolbar setItems:toolbarButtons];
}
}
Appreciate your help with this.
Check the following:
If the images are stored inside your image assets, make sure they are template images.
Then you can either A) set their tint colour in Interface Builder to the grey colour you prefer or B) do it programmatically in your makeBarButton method as such;
button.tintColor = [UIColor grayColor];

Creating a level select screen with circular buttons

I'm trying to create a level select screen for a game I'm making which consists of a grid of circular buttons with level numbers in them. At any one time one button should be selected and displayed with a filled background instead of just an outline.
My initial implementation was to create a view which looked something like this:
#implementation LevelView
- (void)drawRect:(CGRect)rect {
int i = 1;
for (int row = 0; row < NUM_ROWS; row++) {
for (int col = 0; col < NUM_COLS; col++) {
// Calculate frame of the circle for this level.
if (i == selected) {
// Use CoreGraphics to draw filled circle with text.
}
else {
// Use CoreGraphics to draw outlined circle with text.
}
i++;
}
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
for (UITouch *touch in touches) {
// Get the level that the touch is in the circle of.
int level = [self levelForPoint:[touch locationInView:self]];
selected = level;
[self setNeedsDisplay];
}
}
#end
The problem with this is that the selecting and unselecting of the various buttons are not animated (fade in, fade out) like the circular buttons in the iOS7 lock screen or Phone app.
Is there a better/easier way that I can accomplish this?
In response to your request to fade button state: we subclassed UIButton and overrode the setHighlighted: method like so:
- (void) setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
if (highlighted) {
[self setBackgroundColor:[UIColor whiteColor]];
} else {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.4f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[self setBackgroundColor:nil];
[UIView commitAnimations];
}
}
As promised here is my way of tackling the problem. Let's first create a grid of UIButtons in viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
for (NSUInteger i = 0; i < 10; ++i)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag = i;
[button setImage:[UIImage imageNamed:#"normal_image"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"selected_image"] forState:UIControlStateSelected];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
//You can assign a button frame here or in viewWillLayoutSubviews or viewDidLayoutSubviews
button.frame = CGRectMake(your...frame);
[self.view addSubview:button];
[buttons addObject:button]; //NSMutableArray ivar containing our buttons
}
And that takes care of placing our buttons. You can use 2 for loops to make a grid of your choice. As for the image I'm assuming that you're using pre-baked images (you can pre-bake for retina and non retina separately). If you want to render it programatically let me know and I will edit the post.
As for the selection logic we implement the selector buttonTapped:
- (void)buttonTapped:(UIButton *)sender
{
for (UIButton *button in buttons) //Resets all selected states if there were any
button.state = UIControlStateNormal;
sender.state = UIControlStateSelected;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Hey" message:[NSString stringWithFormat:#"You touches the %ldth button.", (long)sender.tag + 1] delegate:nil cancelButtonTitle:#"Cool" otherButtonTitles:nil]
[alert show];
}
EDIT as for the animation purposes you can take a look at the link here or you can use an array of imageViews with attached UITapGestureRecognizers.
EDIT 2 A basic way on how to draw images for your buttons:
UIGraphicsBeginImageContextWithOptions(CGSizeMake(buttonWidth, buttonHeight), NO, 0.0f); //Makes a graphics context that is not opaque and uses the screen scale (so you do not need to worry about retina or non retina)
[[UIColor redColor] set];
UIRectFill(CGRectMake(0.0f, 0.0f, buttonWidth, buttonHeight);
NSDictionary *drawingAttributes = #{NSFontAttributeName: [UIFont systemFontOfSize:15.0f], NSForegroundColorAttributeName: [UIColor blueColor]};
[#"Normal state" drawAtPoint:CGPointZero withAttributes:drawingAttributes];
UIImage *normalStateImate = UIGraphicsGetImageFromCurrentImageContext();
[[UIColor greenColor] set];
UIRectFill(CGRectMake(0.0f, 0.0f, buttonWidth, buttonHeight);
[#"Selected state" drawAtPoint:CGPointZero withAttributes:drawingAttributes];
UIImage *selectedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
And then you would apply the images in the same way as I have written above. How to draw pretty images and whatnot is another topic and if you want help on that I suggest googling or asking another question. If you ever feel like changing the images you would redraw them and apply them again to your buttons.

UIButton suddenly stops working

So I'm having issues with the buttons for the filters in my photo app. I'm not exactly sure why, but they've suddenly stopped working. The selector is not being called when they're tapped, but I have no idea why. They were working just fine a few days ago, but for some reason they're not now. I've checked the UIView subviews array, verified that it's right on top. I need a way to see if, for some reason, the touches are not making it to the button. I'm not sure how to do this.
I wish I had more information to give, but this is all I've got. I hope someone has some suggestions because I'm at wit's end with it.
Thanks in advance!
Button Creation Method:
-(void)createFilterButtonsForThumbnail:(UIImage *)thumbnail
{
UIView *filtersContainer = self.filterContainer;
CGRect buttonFrame = CGRectMake(kFilterFrameThickness, kFilterFrameThickness,
thumbnail.size.width, thumbnail.size.height);
CGFloat frameWidth = thumbnail.size.width+(2*kFilterFrameThickness);
CGFloat frameHeight = kFilterPickerHeight;
UIEdgeInsets backgroundInsets = UIEdgeInsetsMake(0, kFilterFrameThickness, 0, kFilterFrameThickness);
UIImage *buttonBackgroundImage = [[UIImage imageNamed:#"FilmReel"] resizableImageWithCapInsets:backgroundInsets];
for (int i = 0;i<(self.filterPaths.count+kFilterNonLookups+1);i++){
UIImageView *buttonBackground = [[UIImageView alloc] initWithFrame:CGRectMake(kFilterSidePadding+(i*(frameWidth+kFilterSidePadding)),
0,
frameWidth,
frameHeight)];
[buttonBackground setImage:buttonBackgroundImage];
UIButton *thumbnailButton = [[UIButton alloc] initWithFrame:buttonFrame];
UIImage *filteredThumbnail = [self applyFilterAtIndex:i ToImage:thumbnail];
[thumbnailButton setImage:filteredThumbnail forState:UIControlStateNormal];
[thumbnailButton addTarget:self action:#selector(filterSelected:) forControlEvents:UIControlEventTouchUpInside];
[thumbnailButton setTag:i];
[buttonBackground addSubview:thumbnailButton];
[filtersContainer addSubview:buttonBackground];
if ((i > (kFilterProMinimumIndex)) && ([self isProVersion]) == NO){
UIImageView *proTag = [[UIImageView alloc] initWithImage:nil];
CGRect proFrame = CGRectMake(buttonFrame.origin.x,
buttonFrame.origin.y + buttonFrame.size.height - kFilterProIconHeight-kFilterFrameThickness,
kFilterProIconWidth,
kFilterProIconHeight);
[proTag setBackgroundColor:[UIColor orangeColor]];
[proTag setFrame:proFrame];
[thumbnailButton addSubview:proTag];
[self.filterProTags addObject:proTag];
}
}
}
Selector Method:
-(void)filterSelected:(UIButton *)button
{
NSLog(#"Pressed button for index %i",button.tag);
int buttonTag = button.tag;
if ((buttonTag < kFilterProMinimumIndex+1) || ([self isProVersion] == YES)){
[self.imageView setImage:[self applyFilterAtIndex:buttonTag ToImage:self.workingImage]];
}
else {
[self processProPurchaseAfterAlert];
}
}
I see a couple issues in this, but I'm not sure which ones are causing it to break. First, you should instantiate your button using buttonWithType: on UIButton:
UIButton *thumbnailButton = [UIButton buttonWithType:UIButtonTypeCustom];
[thumbnailButton setFrame:buttonFrame]
The reason for this is UIButton is a class cluster, and you should let the OS give you the correct button.
Secondly, you're adding the Button as a Subview of an UIImageView, which by default, has userInteractionEnabled set to NO.
This property is inherited from the UIView parent class. This class
changes the default value of this property to NO.
You should have the button be one large button, with the background of the button be the image you want it to be.

Animated Images in a UIButton

What is the best way to animate images of a button?
Say I want to cycle through like 6 frames and have the user still be able to click the button?
Should I animate the images and just have an invisible button on top of it in interface builder?
Is there a way to animate them within defining the UIButton?
Or is it better to animate images and find the users touch point and act on that?
You can do this using the imageView property of UIButton. This will allow the button to behave as it normally would with your images animating on it. Here's an example:
NSMutableArray *imageArray = [NSMutableArray new];
for (int i = 1; i < 4; i ++) {
[imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:#"%d.png",i]]];
}
[myButton setImage:[UIImage imageNamed:#"1.png"] forState:UIControlStateNormal];
[myButton.imageView setAnimationImages:[imageArray copy]];
[myButton.imageView setAnimationDuration:0.5];
[myButton.imageView startAnimating];

Resources