PNG image displays half transparent in ios - ios

I have subclassed an UITextField, so that there is an icon next to the text field, here is the code:
iconView = [UIImageView new];
self.textField = [UITextField new];
self.textField.borderStyle = UITextBorderStyleNone;
self.textField.textColor = [UIColor whiteColor];
self.layer.cornerRadius = 5;
self.layer.borderWidth = 1;
self.layer.borderColor = TEXTFIELD_BORDER_COLOR;
self.backgroundColor = [UIColor clearColor];
self.textField.backgroundColor = [UIColor clearColor];
//iconView.backgroundColor = [UIColor clearColor];
[self.textField setTranslatesAutoresizingMaskIntoConstraints:NO];
[iconView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self setTranslatesAutoresizingMaskIntoConstraints:NO];
[self addSubview:iconView];
[self addSubview:self.textField];
[self setUpConstraints];
When I set an image, which doesn't have any transparency inside, everything works fine, but when I set with transparency I get faint, half visible image.
Here are two examples of image, first displays fine, second is barely visible:

The problem is with your image.
In the first image (the person one), the gray colored area is opaque. And in the second image (the lock), the gray colored area is almost transparent (opacity is at 31%).

Related

IOS/Objective-C: Change tint of an image view

I would like to change the color of an image view when the user touches it. I don't need it to change a great deal. However, I would like it to change to a shade of blue. I found the following code to change the gradient but it is not having any effect. I don't need a gradient, actually, only a tint. Would appreciate any suggestions.
UIView *snapshot = [[UIImageView alloc] initWithImage:image];
snapshot.layer.masksToBounds = NO;
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.colors = #[(id)[UIColor redColor].CGColor, (id)[UIColor blackColor].CGColor];
[snapshot.layer insertSublayer:gradient atIndex:0];
An easy and non-intrusive way is to add a translucent overlay to your image view. This can then be shown and hidden as the button is pressed and released.
- (void)indicatedImageViewPressed:(UIImageView *)imageView on:(BOOL)on {
UIView *overlay = [imageView viewWithTag:1]; // See if already created
if (overlay == nil) {
overlay = [[UIView alloc] initWithFrame:imageView.bounds];
overlay.tag = 1; // Mark view to find it next time
overlay.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:0.3]; // Your color overlay goes here
[imageView addSubview:overlay];
}
overlay.hidden = !on;
}
You can try giving the UIImage a tint
UIImage *image = [[UIImage imageNamed:#"Your Image"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.tintColor = [UIColor clearColor]; // Or any color really you'd like would work
imageView.image = newImage;
And then changing that tint when the user touches the image using a tap gesture recognizer

Animate UILabel to Change Colour?

I would like to make the text in my UILabel called coloursLabel change text colour by itself. I have tried the following method as another SO answer suggested (albeit for the backgroundColor) but it still doesn't change. Am I missing something silly?
UILabel *coloursLabel = [[UILabel alloc] init];
coloursLabel.text = #"This sentence is colourful!";
[coloursLabel sizeToFit];
coloursLabel.center = self.view.center;
coloursLabel.frame = CGRectOffset(coloursLabel.frame, timeForPage(6), 0);
[UIView animateWithDuration:2.0 animations:^{
coloursLabel.textColor = [UIColor greenColor];
coloursLabel.textColor = [UIColor redColor];
coloursLabel.textColor = [UIColor blueColor];
coloursLabel.textColor = [UIColor yellowColor];
coloursLabel.textColor = [UIColor blackColor];
} completion:NULL];
coloursLabel shows the correct text but the text is in black.
Any help would be much appreciated!
UILabel's textColor is not animatable. You could write your own Label class, using a CATextLayer as mask and animate its layer's backgroundColor.

Why and how is this transparent portion shown as opaque in iPhone App?

Please check below image.
If you see, below the black outline of the blue speech bubble, there is a skybluish portion shown.
I am using a transparent png for the blue speech bubble. So, not sure from where this skyblue portion comes.
Please is the transparent PNG of the speech bubble that I use.
What is wrong here?
UIView* popoverView1;
UIViewController* popoverContent;
UIImageView *imagebackground;
#property (nonatomic,retain) IBOutlet UIPopoverController *pc;
popoverContent = [[UIViewController alloc]init];
popoverView1 = [[UIView alloc]initWithFrame:CGRectMake(56,0,393,444)];
// popoverView1.backgroundColor = [UIColor whiteColor];
imagebackground = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,393,444)];
[imagebackground setImage:[UIImage imageNamed:#"card-creation-screen15.png"]];
[popoverView1 addSubview:btndone];
[popoverView1 addSubview:btn];
popoverContent.view = popoverView1;
[btndone setHidden:YES];
[popoverView1 addSubview:imagebackground];
[popoverView1 bringSubviewToFront:btn];
[popoverView1 bringSubviewToFront:btndone];
popoverController.backgroundColor = [UIColor clearColor];
popoverView1.backgroundColor = [UIColor clearColor];
popoverContent.contentSizeForViewInPopover = CGSizeMake(393, 444);
pc = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
[pc presentPopoverFromRect:CGRectMake(20,650,131,131) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown
animated:YES];
pc.backgroundColor = [UIColor clearColor];
popoverController.backgroundColor = [UIColor clearColor];
popoverView1.backgroundColor = [UIColor clearColor];
pc.backgroundColor = [UIColor clearColor];
Your popoverview1 view has a white backgroundColor, that should be [UIColor clearColor]; in any case - you also see a small white border on top of your image because of that. Where the gradient comes from I can't explain from the information you gave.

Colour changed in iOS7.1, how to change searchBar colour?

On iOS7.0.3 - 7.0.6, my searchBar colour is Gold/yellow colour like this:
But on iOS 7.1, colour becomes like this:
I set
searchBar.tintColor = [UIColor clearColor];
searchBar.backgroundColor = goldColor;
searchBar.tintColor = [UIColor blackColor];
I've tried so many ways and all are failed. Can anyone figure out what changes in iOS 7.1?
============== My fix ===============
I fix this problem by covering a view on searchBar and add the search text filed as subview on this new view.
I need point out that the gold status bar is a subView of searchBar, and it's frame is CGRectMake(0, -20, 320, 20) and it's background colour is gold.
At first, I set this:
_searchBar.translucent = YES;
_searchBar.scopeBarBackgroundImage = [self imageWithColor:UWGold];
and looks like this:
Then, I expand the view cover the status bar, I changed the view's frame.size.height + searchBar's height, then use this line:
UITextField *textSearchField = [_searchBar valueForKey:#"_searchField"];
to get the textSearchField, then add this textSearchField to the cover view.
At last, the searchBar is exactly like when on iOS 7.0
Not a good way, I need figure out what changes on iOS 7.1 and use a right way to implement this.
Try this:
if(IOS_7)
{
self.searchBar.searchBarStyle = UISearchBarStyleMinimal;
self.searchBar.backgroundImage = [UIImage imageWithColor:[UIColor redColor] cornerRadius:5.0f];
}
Hopefully this will help you.
None of the above answers worked for me on iOS 7/8. Here's some setup code that did the trick:
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 44)];
searchBar.scopeButtonTitles = #[#"Scope1", #"Scope2"];
searchBar.selectedScopeButtonIndex = 0;
searchBar.backgroundColor = [UIColor clearColor];
searchBar.barTintColor = [UIColor clearColor];
searchBar.translucent = YES; // SUPER IMPORTANT, REMOVING THIS MESSED UP THE SCOPE BAR
// ONLY USE IMAGES, NOT BACKGROUND COLORS
UIImage *searchBarBackgroundImage = [[UIImage imageNamed:#"SearchBarBackgroundImage"];
UIImage *scopeBarBackgroundImage = [[UIImage imageNamed:#"ScopeBarBackgroundImage"];
[searchBar setBackgroundImage:searchBarBackgroundImage
forBarPosition:UIBarPositionAny
barMetrics:UIBarMetricsDefault];
searchBar.scopeBarBackgroundImage = scopeBarBackgroundImage;
searchBar.tintColor = [UIColor whiteColor];
I used next approach for changing backgroundColor of searchBar
1.As suggested by #Ben Jackson - set BackgroundImage
[self.searchBar setBackgroundImage:[self imageWithColor:[UIColor blueColor]] forBarPosition:UIBarPositionAny
barMetrics:UIBarMetricsDefault];
2.Change textField to what u need according to design
NSArray *searchBarSubViews = [[self.searchBar.subviews objectAtIndex:0] subviews];
for( int i = 0; i < searchBarSubViews.count; i++) {
if([[searchBarSubViews objectAtIndex:i] isKindOfClass:[UITextField class]]) {
UITextField *searchTextField = (UITextField *)[searchBarSubViews objectAtIndex:i];
[searchTextField setTintColor:[UIColor blueColor]];
searchTextField.placeholder = #"Search";
searchTextField.backgroundColor = [UIColor whiteColor];
searchTextField.layer.borderColor = [UIColor blueColor].CGColor;
searchTextField.layer.borderWidth = 1.0f;
searchTextField.layer.cornerRadius = 8.0f;
searchTextField.leftViewMode = UITextFieldViewModeNever;
}
}
Also method for image from color - here
Result:

Adding a badge icon to round rect button?

Is there a convenient way to make a button with a badge? Not on the icon... I want to do it within the app. A button or an image with a badge selected by the program. Or do I have to build a library of photoshop images?
You'll need to use resizableImageWithCapInsets to achieve this without a library of photoshop images. There are some great threads (here and here) that explain its use.
Here's an example I just made to give you an idea:
//Create a label (width/height not important at this stage)
UILabel *yourLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
yourLabel.text = #"7+";
[yourLabel sizeToFit];
CGRect labelFrame = yourLabel.frame;
//Here we create a UIImage that is resizable, but will not resize the areas concerned with the cap insets you've defined
UIImage *badgeImage = [[UIImage imageNamed:#"badge.png"]resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
UIImageView *badgeImageView = [[UIImageView alloc]initWithImage:badgeImage];
badgeImageView.contentMode = UIViewContentModeScaleToFill;
badgeImageView.backgroundColor = [UIColor clearColor];
labelFrame.size.width += 5; //This is the 'padding' on the right and left (added together)
//If your badge edges are completely circular then you don't want to change the height, but if they're not then go ahead in the same way with the width. If your badge has a static height, you'll need to make sure the font size doesn't exceed this height; better start off with a small font-size
badgeImageView.frame = labelFrame; //The badge is now the right width with padding taken into account
//Center the label on the badge image view
yourLabel.center = CGPointMake(badgeImageView.frame.size.width/2, badgeImageView.frame.size.height/2);
//Finally we add the label to the badge image view
[badgeImageView addSubview:yourLabel];
//Add your badge to the main view
[self.view addSubview:badgeImageView];
[badgeImageView release];
[yourLabel release];
UILabel *lbl_notification_count = [[UILabel alloc]initWithFrame:CGRectMake(5,0, 18, 18)];
lbl_notification_count.textColor = [UIColor whiteColor];
lbl_notification_count.textAlignment = NSTextAlignmentCenter;
lbl_notification_count.text = [NSString stringWithFormat:#"%d",appDel.NotificationBadge];
lbl_notification_count.adjustsFontSizeToFitWidth=YES;
lbl_notification_count.layer.borderWidth = 1;
lbl_notification_count.layer.cornerRadius = lbl_notification_count.layer.frame.size.height/2;
lbl_notification_count.layer.masksToBounds = YES;
lbl_notification_count.layer.borderColor =[[UIColor colorWithRed:241.0/255.0 green:84.0/255.0 blue:67.0/255.0 alpha:1.0] CGColor];
lbl_notification_count.backgroundColor = [UIColor colorWithRed:241.0/255.0 green:84.0/255.0 blue:67.0/255.0 alpha:1.0];
lbl_notification_count.font = CustomFontMediumWithSize(12);
if (appDel.NotificationBadge >= 1) {
[btnNotification addSubview:lbl_notification_count];
[lbl_notification_count setHidden:NO];
}else{
[lbl_notification_count setHidden:YES];
}

Resources