UIStepper Customization Results in Blue Rectangles - ios

I've been trying to customizing the looks of a UIStepper object added over Interface Builder. In reference to a topic here and others, I've added a few lines of code to viewDidLoad.
- (void)viewDidLoad {
[[UIStepper appearance] setIncrementImage:[UIImage imageNamed:#"stepperupImage.tif"] forState:UIControlStateNormal];
[[UIStepper appearance] setDecrementImage:[UIImage imageNamed:#"stepperdownImage.tif"] forState:UIControlStateNormal];
}
The result is the blue rectangles of death as shown below. UIStepper doesn't have a property like UIButtonTypeCustom for UIButton. Does anybody know how to fix this problem? My deployment target is iOS 8.0.
Thanks.

To render images that don’t have transparency as they originally were, either:
Change the “Render As” setting to “Original Image”(.xcasset files only):
— or —
Change the rendering mode in code (docs):
UIImage *image;
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
In your case,the code would look like this:
[[UIStepper appearance] setIncrementImage:[[UIImage imageNamed:#"stepperupImage.tif"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
[[UIStepper appearance] setDecrementImage:[[UIImage imageNamed:#"stepperdownImage.tif"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];

UIImage *incrementImageFromFile = [UIImage imageNamed:#"plus.png"];
UIImage *incrementImage = [incrementImageFromFile imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *decrementImageFromFile = [UIImage imageNamed:#"minus.png"];
UIImage *decrementImage = [decrementImageFromFile imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
// For Normal state
[stepper setIncrementImage:incrementImage forState:UIControlStateNormal];
[stepper setDecrementImage:decrementImage forState:UIControlStateNormal];
// For Highlighted state
[stepper setIncrementImage:incrementImage forState:UIControlStateHighlighted];
[stepper setDecrementImage:decrementImage forState:UIControlStateHighlighted];

Related

How to keep tintColor in a UIButton then click on this?

I'm, setting the tintColor to the UIImageView of a UIButton like this:
UIImage* img = [UIImage imageNamed:imageName];
icon.image = [img imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
icon.tintColor = [UIColor greenColor];
This code is working, but then Click the UIButton, the color return to the initial color of UIImage.
How could I keep the green color added?
Thanks!
Try like this if help.
UIImage *image = [[UIImage imageNamed:#“imageName.png”] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[_yourBtn setImage:image forState:UIControlStateNormal];
[_yourBtn setTintColor:[UIColor greenColor]];

iOS > how to remove white marks to the right of custom back button

I want to custom iOS back button throughout the application. So I add these lines in my file AppDelegate.m :
UIImage *backButton = [UIImage imageNamed:#"btn_return"];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[backButton resizableImageWithCapInsets:UIEdgeInsetsMake(0, backButton.size.width, 0, 0)] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
Here is the result :
As you can see, there is a white mark on the right.... Does somebody know why ? How can I remove it ?
The previous view controller has a white space as back button title (in his navigation item) because I don't want any label.
Maybe it's because of that ?! Is there an other solution to not see the default "Back" label ?
Thanks a lot for your help
[EDIT]
When I try the answer of Cy-4AH, I get :
Use in this way for
UIImage *image1 = [[UIImage imageNamed:#"btn_return"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[self.menuBarBtn setImage:image1];
You need use
[[UINavigationBar appearance] setBackIndicatorImage: backButton];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage: backButton];
if yours deployment target IOS 7+
Otherwise ask yours designer to add one pixel empty column in the right.
Use this code:
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
forBarMetrics:UIBarMetricsDefault];
UIButton *backButton;
backButton=[[UIButton alloc]initWithFrame:CGRectMake(0, 0,35 ,35)];
backButton.imageEdgeInsets = UIEdgeInsetsMake(7, 7, 7, 7);
[backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(btnBackAction:) forControlEvents:UIControlEventTouchUpInside];
[backButton setImage:[UIImage imageNamed:#"your back button image name"] forState:UIControlStateNormal];
UIBarButtonItem *Rightbarbutton=[[UIBarButtonItem alloc]initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem=Rightbarbutton;
Use this code for hide default backbutton
self.navigationItem.hidesBackButton = YES;
Thanks to #DipenPanchasara, I changed my code by :
UIImage *backButtonImage = [[UIImage imageNamed:#"btn_return"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
GFloat leftInset = SYSTEM_VERSION_GREATER_THAN(#"9.0") ? -2.5 : backButtonImage.size.width -12;
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[backButtonImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, leftInset, 0, 0)] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
(values in UIEdgeInsetsMake changed)
Here is the result :
The back button image is slightly flattened but it doesn't matter.
Thank you all for your suggestions ! :-)

UISlider setMaximumTrackTintColor in iOS 7.1

[slider setMaximumTrackTintColor: color]
has unexpected results in iOS 7.1 (the slider bar changes its position appearing at top instead of vertical center or disappears completely), while working fine with prior versions.
[slider setMinimumTrackTintColor: color]
does render the expected result.
This question might be related: UISlider setMaximumTrackTintColor,
but no answer so far.
Update:
I get this: instead of:
Update #2:
Using setMaximumTrackImage might work, but the solution I'm looking for is a way to set any random color and not a preexisting image.
Update #3:
This issue is still present in iOS 7.1.1.
Found this workaroud:
Create a 1x1px UIImage from a UIColor on the fly:
CGRect rect = CGRectMake(0, 0, 1, 1);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
[color setFill];
UIRectFill(rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
and then
[slider setMaximumTrackImage:image forState:UIControlStateNormal];
Looks like an expensive solution but it gets the job done.
Try this out:
UIImage *sliderLeftTrackImage = [[UIImage imageNamed:#"LeftImage.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
UIImage *sliderRightTrackImage = [[UIImage imageNamed:#"RightImage.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[sliderName setMinimumTrackImage: sliderLeftTrackImage forState: UIControlStateNormal];
[sliderName setMaximumTrackImage: sliderRightTrackImage forState: UIControlStateNormal];
If you want to change thumb image the following code will work:
[sliderName setThumbImage:[UIImage imageNamed:#"ThumbImgName.png"] forState:UIControlStateNormal];
[sliderName setThumbImage:[UIImage imageNamed:#"ThumbImgName.png"] forState:UIControlStateHighlighted];
In same way, you can also use color instead of images.
I've created 2px images with the colour of slider track.
And then I set they as tracking images (here's with thumb image for iPad and iPhone)
UIImage *thumbImage = [UIImage imageNamed:#"slider"];
UIImage *trackImage;
if (isiPad) {
[[UISlider appearance] setThumbImage:thumbImage forState:UIControlStateNormal];
[[UISlider appearance] setThumbImage:thumbImage forState:UIControlStateHighlighted];
trackImage = [UIImage imageNamed:#"menu-bar-ipad"];
}
else {
[[UISlider appearance] setThumbImage:[self imageWithImage:thumbImage scaledToSize:CGSizeMake(27, 27)] forState:UIControlStateNormal];
[[UISlider appearance] setThumbImage:[self imageWithImage:thumbImage scaledToSize:CGSizeMake(27, 27)] forState:UIControlStateHighlighted];
trackImage = [UIImage imageNamed:#"menu-bar"];
}
[[UISlider appearance] setMinimumTrackImage:trackImage forState:UIControlStateNormal];
[[UISlider appearance] setMaximumTrackImage:trackImage forState:UIControlStateNormal];
And that's all. Same solution as msmq I guess. And you can see both ways how to make a large image - two images way and scaling way.
Update
Reported this bug and it's already well known. Hopefully they will fix it soon...
Maybe a little bit late, but - here is the answer
You should read description of setter:
The color used to tint the standard maximum track images. Setting this
property removes any custom maximum track images associated with the
slider.
Also this applicable only for maximum color, because minimum setter just change titntColor:
The color used to tint the standard minimum track images.
This mean that slider use some image for maximum track and if u just set tint color nothing will be changes (nothing can't be tinted).
Solution (thanks to #user623396):
UIImage *currentSliderMaximumImage = self.slider.currentMaximumTrackImage;
CGRect rect = CGRectMake(0, 0, currentSliderMaximumImage.size.width, currentSliderMaximumImage.size.height);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
[[[DynamicUIService service] currentApplicationColor] setFill];
UIRectFill(rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.slider setMaximumTrackImage:image forState:UIControlStateNormal];
As result u will get

Custom UIBarButtonItem

I am trying to create a custom UIBarbuttonItem that uses just a png with transparency so that I only have an icon as button. When I try to set the button image, set the background as white, and set the style to Plain I still get an inner shadow and black border around it.
What gives?
I have tried the below code and it still puts the black border around it.
UIImage *background = [UIImage imageNamed:#"Dismiss_normal.png"];
UIImage *backgroundSelected = [UIImage imageNamed:#"Dismiss_selected.png"];
self.closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.closeButton addTarget:self action:#selector(closeButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; //adding action
[self.closeButton setBackgroundImage:background forState:UIControlStateNormal];
[self.closeButton setBackgroundImage:backgroundSelected forState:UIControlStateSelected];
self.closeButton.frame = CGRectMake(0 ,0,background.size.width, background.size.height);
self.closeButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.closeButton];
self.navigationItem.leftBarButtonItem = self.closeButtonItem;
What I noticed is if I do a modal segue the button, with the code above still has a black border around it, but if I do a push segue it doesn't? WTF?
You must set the button type to Custom and the icon image to the button background.
example code:
UIImage *background = [UIImage imageNamed:#"icon.png"];
UIImage *backgroundSelected = [UIImage imageNamed:#"icon_selected.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside]; //adding action
[button setBackgroundImage:background forState:UIControlStateNormal];
[button setBackgroundImage:backgroundSelected forState:UIControlStateSelected];
button.frame = CGRectMake(0 ,0,35,35);
Then set this button to your BarButtonItem like this:
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = barButton;
I understood your problem.
When you actually push a UIViewController it will be a part of existing UINavigationController. So the UINavigationBar retains. But when you use ModalViewController its just another UIViewController kind of "added" to your existing UIViewController. So the UINavigationBar won't be there.
But you can add another UINavigationBar from xib and add a UIButton. But don't directly add the UIButton, if you do so it will automatically convert to UIBarButton making it bordered.
So first drag a normal UIButton to your UIView(not navigation bar). Change it to custom and add UIImage to it using Attribute Inspector. Then drag that image to your custom created UINavigationBar. It will work as I tried it just now. You can see the screen shot attached below.
This will update ALL back buttons and navigation bars throughout the app.
I have used the Following code to set Custom Navigation back button
UIImage *imgBack = [[UIImage imageNamed:#"nav_back.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(-6, 22, -6, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:imgBack forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
and the Following code for Setting background image on navigation bar.
UIImage *navBackgroundImage = [UIImage imageNamed:#"nav_bar.png"];
[[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];
Try use [UIBarButtonItem initWithCustomView]
UIImage *menuButtonImage = [UIImage imageNamed:#"list.png"];// set your image Name here
UIButton *btnToggle = [UIButton buttonWithType:UIButtonTypeCustom];
[btnToggle setImage:menuButtonImage forState:UIControlStateNormal];
btnToggle.frame = CGRectMake(0, 0, menuButtonImage.size.width, menuButtonImage.size.height);
UIBarButtonItem *menuBarButton = [[UIBarButtonItem alloc] initWithCustomView:btnToggle];
self.navigationItem.leftBarButtonItem = menuBarButton;
I personally like FlatUIKit's approach by just flattening out the entire UI through UIAppearance
Essentially, you'd do something like:
UIImage *backButtonPortraitImage = [UIImage backButtonImageWithColor:color barMetrics:UIBarMetricsDefault cornerRadius:cornerRadius];
UIImage *highlightedBackButtonPortraitImage = [UIImage backButtonImageWithColor:highlightedColor barMetrics:UIBarMetricsDefault cornerRadius:cornerRadius];
[appearance setBackButtonBackgroundImage:backButtonPortraitImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[appearance setBackButtonBackgroundImage:highlightedBackButtonPortraitImage forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
UIImage *buttonImageNormal = [UIImage imageWithColor:color cornerRadius:cornerRadius];
UIImage *buttonImageHightlighted = [UIImage imageWithColor:highlightedColor cornerRadius:cornerRadius];
[appearance setBackgroundImage:buttonImageNormal forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[appearance setBackgroundImage:buttonImageHightlighted forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
Try this code..
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *btnImage = [UIImage imageNamed:#"bg"];
[button setImage:btnImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, btnImage.size.width, btnImage.size.height);
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
UIImage *icon = [UIImage imageNamed:#"icon"]
UIImageView *iconView = [[UIImageView alloc] initWithImage:icon];
iconView.frame = CGRectMake(0, 0, icon.size.width, icon.size.height);
[button addSubview:iconView];
iconView..userInteractionEnabled=NO;
[iconView centerBothDirections];
[iconView release];
UIBarButtonItem *barbtn = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
self.navigationItem.leftBarButtonItem =barbtn

Set a button background image iPhone programmatically

In Xcode, how do you set the background of a UIButton as an image? Or, how can you set a background gradient in the UIButton?
Complete code:
+ (UIButton *)buttonWithTitle:(NSString *)title
target:(id)target
selector:(SEL)selector
frame:(CGRect)frame
image:(UIImage *)image
imagePressed:(UIImage *)imagePressed
darkTextColor:(BOOL)darkTextColor
{
UIButton *button = [[UIButton alloc] initWithFrame:frame];
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[button setTitle:title forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
UIImage *newImage = [image stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
[button setBackgroundImage:newImage forState:UIControlStateNormal];
UIImage *newPressedImage = [imagePressed stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
[button setBackgroundImage:newPressedImage forState:UIControlStateHighlighted];
[button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
// in case the parent view draws with a custom color or gradient, use a transparent color
button.backgroundColor = [UIColor clearColor];
return button;
}
UIImage *buttonBackground = UIImage imageNamed:#"whiteButton.png";
UIImage *buttonBackgroundPressed = UIImage imageNamed:#"blueButton.png";
CGRect frame = CGRectMake(0.0, 0.0, kStdButtonWidth, kStdButtonHeight);
UIButton *button = [FinishedStatsView buttonWithTitle:title
target:target
selector:action
frame:frame
image:buttonBackground
imagePressed:buttonBackgroundPressed
darkTextColor:YES];
[self addSubview:button];
To set an image:
UIImage *buttonImage = [UIImage imageNamed:#"Home.png"];
[myButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:myButton];
To remove an image:
[button setBackgroundImage:nil forState:UIControlStateNormal];
This will work
UIImage *buttonImage = [UIImage imageNamed:#"imageName.png"];
[btn setImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:btn];
In case it helps anyone setBackgroundImage didn't work for me, but setImage did
You can set an background image without any code!
Just press the button you want an image to in Main.storyboard, then, in the utilities bar to the right, press the attributes inspector and set the background to the image you want!
Make sure you have the picture you want in the supporting files to the left.
Swift
Set the button image like this:
let myImage = UIImage(named: "myImageName")
myButton.setImage(myImage , forState: UIControlState.Normal)
where myImageName is the name of your image in your asset catalog.
When setting an image in a tableViewCell or collectionViewCell, this worked for me:
Place the following code in your cellForRowAtIndexPath or cellForItemAtIndexPath
// Obtain pointer to cell. Answer assumes that you've done this, but here for completeness.
CheeseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cheeseCell" forIndexPath:indexPath];
// Grab the image from document library and set it to the cell.
UIImage *myCheese = [UIImage imageNamed:#"swissCheese.png"];
[cell.cheeseThumbnail setImage:myCheese forState:UIControlStateNormal];
NOTE: xCode seemed to get hung up on this for me. I had to restart both xCode and the Simulator, it worked properly.
This assumes that you've got cheeseThumbnail set up as an IBOutlet... and some other stuff... hopefully you're familiar enough with table/collection views and can fit this in.
Hope this helps.
In one line we can set image with this code
[buttonName setBackgroundImage:[UIImage imageNamed:#"imageName"] forState:UIControlStateNormal];
Code for background image of a Button in Swift 3.0
buttonName.setBackgroundImage(UIImage(named: "facebook.png"), for: .normal)
Hope this will help someone.

Resources