Custom progress Bar on iOS - objective-c

How do I create a custom progress bar on iOS?

You did not post the image :) But you can use UISlider with "userInteractionEnabled" set to NO and thumb set to small, transparent image. Here's some code:
UIImage* slider = [UIImage imageNamed:#"empty.png"];
UIImage* leftImage = [UIImage imageNamed:#"left_track.png"];
UIImage* rightImage = [UIImage imageNamed:#"right_track.png"];
[self.slider setThumbImage::slider forState:UIControlStateNormal];
[self.slider setThumbImage::slider forState:UIControlStateSelected];
[self.slider setThumbImage::slider forState:UIControlStateHighlighted];
[self.slider setMinimumTrackImage::leftImage forState:UIControlStateNormal];
[self.slider setMaximumTrackImage::rightImage forState:UIControlStateNormal];
Ofcourse, images left and right must be scalable.

You can have 2 images, the "on" image is on top of the "off" image, and each in their own UIImageView. Enable clipsToBound for the UIImageView containing "on" image. Then you can adjust the width of the bounds for the top image according to the number that controls it. You can also choose to make the change continuous or discreet.

Related

Image aspect ratio change after inserting it into imageview

So I'm trying to load an image from camera into image view. I've got it done, but images aspect ratio changes.
I tried setting content mode for imageview but no effect.
Here is how i do it:
UIImage *picture=[UIImage imageWithData:slika];
imageview = UIViewContentModeScaleAspectFit;
[imageview setImage:picture forState:UIControlStateNormal];
Any ideas?
Is image view your own view? Or it's, for example, cell's default imageView?
And you should write so:
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.image = [UIImage imageNamed:picture];
or
[imageView setImage:picture];
There is no method [imageview setImage:picture forState:UIControlStateNormal]; for UIImageView, it only available for UIButton (and may be for some other UIControls, but UIImageView not subclass of UIControl - it direct subclass of UIView) . But button does not scale images preserving aspect ratio.

BarButtonItem resizing for unknown reason

I have a button that is initialized like that>
self.button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(doAction)];
Later, I am attaching a custom background to it, like that>
[self.button setBackgroundImage:[UIImage imageNamed:#"customNavBar_button_right_enabled"]
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
and puting it onto navbar.
[self.navigationItem setRightBarButtonItem:self.button
animated:NO];
This image has fixed size. I want that button to have fully custom background image with no "intelligent" resizing with UIEdgeInsets.I don't want any resizing. But for some reason, it behaves like its insets would be active and it is resized to have bigger width, with roughly the middle half of picture considerably stretched to provide for the wider button.
Why is this happening? How can I prevent this to happen?
The easiest way to achieve a bar button item with a custom image is by:
Drag a UIButton onto the storyboard.
Set the button Type to Custom and set your image for that button.
Drag the button onto your Toolbar.
The storyboard automatically creates a UIBarButtonItem and adds your UIButton as a subview. Or if you do NOT want to use storyboards you could implement the following:
// Initialize the UIButton
UIImage *buttonImage = [UIImage imageNamed:#"buttonImage.png"];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setImage:buttonImage forState:UIControlStateNormal];
aButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
// Initialize the UIBarButtonItem
UIBarButtonItem aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
// Set the Target and Action for aButton
[aButton addTarget:self action:#selector(aButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
Hope it helps!
EDIT: Do not forget to add the aBarButtonItem to the UIToolbar.

how to set a beahavor for all my buttons

i stared to built an app and i want that all my buttons in my app will look the same.
i write code for one button and i want my other buttons look alike.
here is my button code:
UIImage *butimage = [UIImage imageNamed:#"button1.png"];
UIImage *butimage3 = [UIImage imageNamed:#"button1press.png"];
UIImage *butpress = [butimage3 stretchableImageWithLeftCapWidth:15 topCapHeight:0];
UIImage *butimage2 = [butimage stretchableImageWithLeftCapWidth:15 topCapHeight:0];
[button1 setBackgroundImage:butimage2 forState:UIControlStateNormal];
[button1 setBackgroundImage:butpress forState:UIControlStateHighlighted];
[button1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button1 setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
how can i do it?
thanks
Try this:
[button1 addTarget:self action:#selector(anAction:) forControlEvents:UIControlEventTouchUpInside]
I'm assuming you've created only a button, and assigning it all the images.
If you create more buttons, the code for giving them an action is the above one.
For the anAction: part, create a -(void)anAction:(id)sender (i.e) in order to tell the button to run that specific action.
EDIT
Declare your buttons as IBOutlets, place them in your viewController.xib and link them.
Then declare their properties in viewWillAppear or viewDidLoad (the code you wrote, basically).
Then if you want them to perform some actions, write some -(IBAction)actionForButton:(id)sender and just assign them to the buttons in IB.

UIButton with custom background loses title

I was trying to create a custom button, but after setting the image, I noticed that the title disappears, it think this is because I add an image to the button, which overlaps the title.
UIImage *pickerBg = [UIImage imageNamed:#"btn_picker.png"];
UIImage *pickerBg_highlighted = [UIImage imageNamed:#"btn_picker_highlighted.png"];
[projectBtn setImage:pickerBg forState:UIControlStateNormal];
[projectBtn setImage:pickerBg_highlighted forState:UIControlStateHighlighted];
Is there another way on how to do this?
Use setBackgroundImage, not setImage
UIImage *pickerBg = [UIImage imageNamed:#"btn_picker.png"];
UIImage *pickerBg_highlighted = [UIImage imageNamed:#"btn_picker_highlighted.png"];
[projectBtn setBackgroundImage:pickerBg forState:UIControlStateNormal];
[projectBtn setBackgroundImage:pickerBg_highlighted forState:UIControlStateHighlighted];
[projectBtn setTitle:#"aTitle" forState:UIControlStateNormal];
Use this to set image with title.
Create a UILabel with the title you need, then add it as a subview to your UIButton.
when you create a UIButton, use setImage to set ButtonImage will change title's position,the titleText and the image are at At the same level,the titleText in behind of image,so you can setBackImage, the image will behind the titleText,but ,the image will tension as the button.

How to change UIButton's background image while clicking it?

I have a UIButton in each of my tableview's cells. I want to glow the uibutton while click action happends. I set an image as its background image using the code
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(270,10,30,30);
[button setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
The image.png is little bit darker image. Because i want to show a click effect by showing a glowed image.
Now shall i glow the image.png by code while clicking happends? Or should i create a new glowing image(say image_glow.png) with the same dimension and replace the previous one?
Whatever, please explain how to implement this....
The best way is to use multiple images.
You can set different image to different state of a UIButton by using setImage method.
As i see, you have set image only for UIControlStateNormal state, which makes all other states of a button to use the same image.
reference : UIButton States
Hope this helps.