Change UISlider background with code - ios

I have created a UISlider , and i want to change its look .
I dont want to change its image with my own, like this one
Change iPhone UISlider bar image
but, to create an elliptical UIView of my own,in code, and set it instead of the current slider thin line, than instead of the moving circle button, create a circle UIView in code, and replace it also.
I know i can set their images ,
[self setThumbImage:[UIImage imageNamed:#"switchThumb.png"] forState:UIControlStateNormal];
[self setMinimumTrackImage:[UIImage imageNamed:#"switchBlueBg.png"] forState:UIControlStateNormal];
[self setMaximumTrackImage:[UIImage imageNamed:#"switchOffPlain.png"] forState:UIControlStateNormal];
but how do i set my own costume uiview ?

If you want to make a circular Slider,you can check it once: http://code4app.net/ios/EFCircularSlider/52a6bdd9cb7e84af088b5fa8

Related

Custom keyboard buttons iOS

I am trying to setup a highlighted state for keyboard buttons, but have some difficulties with it:
I can't set backgroundImage to be beneath the spaceButton Image, so it won't overlap shift_1 like on the image below (button is tapped).
I can't set up layer margins, so that there would be a space from the bottom and other buttons, and at the same time that spacing would function as that button.
Below is my code:
[self.spaceButton setImage:[UIImage imageNamed:#"shift_1"] forState:UIControlStateNormal];
[self.spaceButton setBackgroundImage:[KeyboardViewController imageFromColor:[UIColor colorWithWhite:0.4 alpha:0.5]]
forState:UIControlStateHighlighted];
self.spaceButton.layer.cornerRadius = 7.0;
self.spaceButton.layer.masksToBounds = YES;
I would be very thankful if anyone could help me.
You would have two ways:
By Image : Just add transparent pixel at bottom,left,right in image and create new image then set that image as in background image of button.
By imageEdgeInsets : Set button imageEdgeInsets and use to image property for set image(Highlighted/Normal state).

Changing a UIButton's image when it's tapped

I have a UIButton that I am programmatically creating and adding to a UITableViewCell.
I have successfully set it up so that if you tap the button and keep holding it down, it will change to the image that I have set for the "highlighted state", but this is not good enough.
When the user taps the button, I need it to completely change to the new image. If the user taps the button again, I want it to change back to the original image.
I want the image to change every time they tap. Right now it changes if they tap and keep holding it down, but it switches back to the original image as soon as they are done holding it down.
Here is the code that I have so far for my buttons:
UIImage *addFriendButtonImage = [UIImage imageNamed:#"SliderThumb-Normal-G"];
UIImage *addFriendButtonImageHighlighted = [UIImage imageNamed:#"SliderThumb-Normal"];
UIButton *addFriendButton = [[UIButton alloc]init];
addFriendButton.frame = CGRectMake(237, -10, 64, 64);
[addFriendButton setImage:addFriendButtonImage forState:UIControlStateNormal];
[addFriendButton setImage:addFriendButtonImageHighlighted forState:UIControlStateHighlighted];
I also tried setting the state for the new image to "UIControlStateSelected" but all that does it make the original image a little darker. It doesn't even change to the new image, and once again it only shows it's effect if you are holding the button down.
Set the image for both the UIControlStateHighlighted and UIControlStateSelected states:
[addFriendButton setImage:addFriendButtonImage forState:UIControlStateNormal];
[addFriendButton setImage:addFriendButtonImageHighlighted forState:UIControlStateHighlighted];
[addFriendButton setImage:addFriendButtonImageHighlighted forState:UIControlStateSelected];
Then listen for UIControlEventTouchUpInside:
[addFriendButton addTarget:self action:#selector(handleTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
Then update the selected state:
- (void)handleTouchUpInside:(UIButton *)sender {
sender.selected = !sender.selected;
}
What you want is to set new image with [addFriendButton setImage:someImage forState:UIControlStateNormal]; every time the user taps on addFriendButton (look for event UIControlEventTouchUpInside).
If you want you can still assign appropriate highlight image on [addFriendButton setImage:someImage_highlighted forState:UIControlStateHighlighted];
You need to create an IBAction method for your button. If you are using a storyboard to design your view controllers go to the respective view controller in the assistant editor (tap on the icon in the upper right corner of the Xcode window), CTRL+drag the a line from the button to the .h file of your view controller's class like this:
Select Action and enter a named for the method like touchUpInsideButton.
Repeat the same steps to create a property for your button. (But select Outlet instead of Action and give your button a name like myButton.) Now you should have these two lines in your .h file:
#property (strong, nonatomic) IBOutlet UIButton *myButton;
- (IBAction)touchUpInsideButton:(id)sender;
Now go to the .m file of your view controller. Search for the newly created action method - (IBAction)touchUpInsideButton:(id)sender and enter the following code to change the button's image when the user taps the button:
- (IBAction)touchUpInsideButton:(id)sender {
UIImage *myImage = [UIImage imageNamed:#"myImageName"];
[self.myButton setImage:myImage forState:UIControlStateNormal];
}
(You can put any code inside this method that is executed whenever the user taps the button.)
I would recommend this method if you are using a storyboard. If not please refer to #Corey's reply.

Set image of uiButton in custom table cell

I have a uitbutton within a custom table cell. I am trying to set the buttons image with
[cell.thumbImage setImage:[UIImage imageNamed: #"full_breakfast.jpg"] forState:UIControlStateNormal];
It is displaying a blue button instead of the image like so:
Am I doing this wrong? When i remove the uibutton setImage it returns to the normal button. This SHOULD be fairly simple so sorry if it is.
In iOS7 there is new button type called UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0), // standard system button
Check your .xib file and change button type to Custom, or programatically you can do this:
[UIButton buttonWithType:UIButtonTypeSystem];

Why does a UICollectionViewCell with a UIButton have a monochrome/tinted image?

I create a UICollectionView and add a single cell who's only subview is a UIButton. That button has its title and image set. I've verified that the image data is correct in the debugger.
When the button is drawn on screen I see the text and the image however the image looks as if it has been filled with the tint color, obscuring all of the image other than its shape.
What am I missing here to have this show up as a normal button should?
Update
It turns out this is not specific to UICollectionView but rather all UIButtons in iOS7.
iOS 7 makes all images in buttons behave as template images using the alpha channel of the image in concert with the tint color to produce the image (much like the images in a tab bar). There's a new renderingMode property on UIImage which is defaulted to "automatic" which lets the context decide (which is template style for buttons)
This can be circumvented using the new imageWithRenderingMode: method on UIImage:
UIImage* myImage = [UIImage imageNamed:#"Foo.png"];
myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[button setImage:myImage forState:UIControlStateNormal];
The easiest way to avoid this is to use a different UIButtonType. It's UIButtonTypeSystem on IOS 7 that has this behaviour, so you could use a custom button instead:
UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];
[button setImage:myImage forState:UIControlStateNormal];
When the UIButton's background color lightText in this way, it will not close the button image.
UIButton.backgroundColor = UIColor.lightText

Drawing a UIButton/UIBarButtonItem to use parent bar's tintColor?

I've got a bit of a dilemma. It's not a dealbreaker, but I'm interested in a decent answer if there is one.
I've been using a UIButton with a custom subview inside of a UIBarButtonItem (as the bar button item's customView). My custom subview is not a UILabel nor is it a UIImage, which is why I'm doing what I'm doing. This UIBarButtonItem is an item on my navigation bar, and the navigation bar has a tintColor set. I want the UIButton to have that rounded-rect appearance of UIBarButtonItemStyleBordered, which means it should also take on the tintColor of its parent bar.
Current solutions out there have implemented extracting png files from UIKit for UINavigationBarDefaultButton.png and family. This would look great if I weren't setting a tintColor property; instead the button remains that "iOS navy blue", when I want, say, bright orange (not the color I'm using but you get my drift). Which brings me to my dilemma: what's the best way to make a UIButton look and act like that UIBarButtonItem style, including taking on the tintColor property? I can set that property myself on the button; it's no big deal.
Would I want to draw that UIButton's background in CoreGraphics? Is there a framework/library out there that implements this already? If I'm dreaming the impossible, just tell me. I'm not that awesome with doing CoreGraphics by hand yet, but it's definitely not outside the realm of possibility.
If this can't be done, I know I can always take the cool custom UIView I'm attempting to finagle in and just save it off as an image (and thus use UIImage inside of a UIBarButtonItem), but I'd like to see if this is possible.
This is what I'm dealing with (below). The back button looks awesome, but only because I'm not messing with it. I'd like that rightBarButtonItem to use my tintColor that I have set, but in order to give it that UIBarButtonItemStyleBordered appearance with a customView set, I'm forced to make the button use png files for its background. Below is what you see, even with tintColor set on the UIBarButtonItem (since it's using a png).
I did find this question/answer on Stack Overflow, that gets me close enough that I can wing the rest. It's a well-detailed post about how to tint a UIImage with a UIColor; I've pulled it out into a function and I've posted a gist for that right here.
Combined with that, I've defined a category method on UIButton that lets me create a button with that background image, tinted, and I plop that into an empty UIBarButtonItem.
+ (id)buttonWithBarStyleAndTintColor:(UIColor *)tintColor customView:(UIView *)customView {
UIImage *defaultNormal = [UIImage imageNamed:#"UINavigationBarDefaultButton.png"];
UIImage *defaultPressed = [UIImage imageNamed:#"UINavigationBarDefaultButtonPressed.png"];
UIImage *back = [TintImageWithTintColor(defaultNormal, tintColor)
stretchableImageWithLeftCapWidth:5.0
topCapHeight:0.0];
UIImage *pressed = [TintImageWithTintColor(defaultPressed, tintColor)
stretchableImageWithLeftCapWidth:5.0
topCapHeight:0.0];
UIButton *button = [self buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 34.0f, 30.0f);
[button addSubview:customView];
customView.userInteractionEnabled = NO;
customView.frame = CGRectCenterRectInRect(customView.frame, button.frame);
[button setBackgroundImage:back forState:UIControlStateNormal];
[button setBackgroundImage:pressed forState:UIControlStateSelected];
[button setBackgroundImage:pressed forState:UIControlStateHighlighted];
return button;
}
You can try and set the UIButton type to custom and make sure its color is clear, same for your custom view, that way the only visible view will be the UIBarButtonItem tint color.
Also if you are willing to invest, there is this tool called PaintCode that does the code for CoreGraphics for you.

Resources