How can I change the color as well as size of UIProgressBar in iPhone SDK
Assuming you mean a UIProgressView, you can resize it as you would any other view, by setting the frame:
UIProgressView *progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
[progressView setFrame:CGRectMake(0,0,320,10)];
or if you're using the default style, you can set the frame in the init method:
UIProgressView *progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0,0,320,10)];
As far as I know, the color of a UIProgressView cannot be changed. You'll probably have to roll your own view for that.
As Can noted, there is very little customization available for UIProgressView... pretty sucky. The solution I just implemented was to hack a UISlider. Here's the inspiration, from Apple's support forum.
Create a new UISlider
Set the setMinimumTrackImage and setMaximumTrackImage to your desired UIImage values
Set the setThumbImage to nil
Set the upper and lower bounds of the slider
Once this is setup and added to your UIView, use your custom method to set the value of the slider to the value you would normally have assigned to the UIPRogressView's progress.
You could probably also hack one together using an image that looks like the progress bar at it's smallest size and then create a stretchable image with it.
UIImage *progressBarImg= [someImage stretchableImageWithLeftCapWidth:5.0 topCapHeight:0.0];
Then you could set the image as the backgroundImage for a disabled UIButton and animate the button width incrementally to indicate progress. I haven't tried this, but I think it would work.
Related
I need to crop a UIImage, taken with the device camera, so that it fits inside another UIImage, which represents a frame (with rounded borders and so on). Check the image below:
Using Aspect Fill
Using Aspect Fit
So, what I need is to remove the image excess that is out of the frame bounds.
I tried using UIBezierPath, CGImageRef and other methods that I Googled but I'm not finding a solution for this.
In Interface Builder, use the following configuration:
There are two important settings, namely:
Mode: Aspect Fill
Clip Subviews
It can also be done programmatically:
[imageView setContentMode:UIViewContentModeScaleAspectFill];
[imageView setClipsToBounds:YES];
This will correctly fill the view with the image, keep its aspect ratio and hide what doesn't fit.
make a IBOutlet in your controller.
#property (retain)IBOutlet UIImageView* imageView;
and in -(void) viewDidLoad set
imageView.layer.masksToBounds = YES;
In interface Builder, access the Mode menu inside of the detail pane (the fourth one) and choose the right one for your UIImageView (I guess "center" or "aspect fit").
OLD ANSWER:
You can use the contentGravity property of CALayer to make it work
A constant that specifies how the layer's contents are positioned or scaled within its bounds.
#property(copy) NSString *contentsGravity
Use an UIImageView and do
imageView.contentMode = UIViewContentModeScaleAspectFit;
I need to create a custom vertical slider for my iPhone app, simply because the UISlider is to narrow. It is going to be used as a throttle "stick" for my remote controlled helicopter.
I managed to rotate the slider in xcode with
throttleSlider.transform = CGAffineTransformMakeRotation(3*M_PI_2);
but I think this is quite ugly since the slider still appear horizontally on my storyboard. Also, I can not find anyway to make the slider wider.
How will I approach this problem?
Thank you.
in the application I am developing right now we needed a custom slider. It had to be wider (44pixels wide, the default size of a table view cell). We accomplished our goal by using stretchable images and adjusting the properties of the slider:
UIImage* sliderCenterImage = [UIImage imageNamed:#"sliderCenter.png"];
[slider setThumbImage:sliderCenterImage forState:UIControlStateNormal];
UIImage *leftStretch2 = [[UIImage imageNamed:#"sliderLeft.png"]
stretchableImageWithLeftCapWidth:5.0 topCapHeight:0.0];
slider setMinimumTrackImage:leftStretch2 forState:UIControlStateNormal];
UIImage *rightStretch = [[UIImage imageNamed:#"sliderRight.png"]
stretchableImageWithLeftCapWidth:1.0 topCapHeight:0.0];
[slider setMaximumTrackImage:rightStretch2 forState:UIControlStateNormal];
This way I got an slider that fulfills a whole cell of a tableView.
thumbImage is the image that represents the middle of the slider (the one you interact with, by default a round dot), minimumTrackImage will be the part on the left of the slider (the one that by default is blue), and maximumTrackImage is the part usually left blank on the right of the center of the slider. There are more properties of the UISlider that allow further customization. Take a look at UISlider developer reference
Hope this helps.
I've used this implementation of UICustomSwitch before, from Catamount Software's blog:
UICustomSwitch
It attempts to customize the familiar UISwitch class, but it accomplishes this by subclassing UISlider, and replacing the normal switch imagery with custom images. You could download this code, then just replace the included images with something that you draw yourself.
Because this was intended to be a switch, which only has on and off settings, you'll see that it responds to touches with this:
[self setOn:on animated:YES];
Just get rid of the code that calls setOn:animated: so that the slider doesn't force its value all the way to 0.0 or 1.0.
As an updated answer, stretchableImageWithLeftCapWidth is deprecated. It is advised to use
resizableImageWithCapInsets
For a custom slider in swift:
var thumb = UIImage(named: "slider_thumb")
musicSlider.setThumbImage(thumb, forState: UIControlState.Normal)
var left = UIImage(named: "left_slide")?.resizableImageWithCapInsets(UIEdgeInsets(top: 0.0,left: 0.0,bottom: 0.0,right: 0.0))
musicSlider.setMinimumTrackImage(left, forState: UIControlState.Normal)
var right = UIImage(named: "right_slide")?.resizableImageWithCapInsets(UIEdgeInsets(top: 0.0,left: 0.0,bottom: 0.0,right: 0.0))
musicSlider.setMaximumTrackImage(right, forState: UIControlState.Normal)
The UIEdgeInserts specify the buffer of the image that won't get resized. so left:15.0 would mean the area 15 pixels in from the left will not get resized. In my case I didn't care so it was all zero.
As a note, the images themselves are what is making the slider bar wider. My thumb image is 35 pixels in height, my left and right slider images are 20 pixels in height. All are sliced and resizable from Images.xcassets (where images should be kept)
I've had trouble getting this to work, nowhere have I seen a working example on the web. Now offering bounty on this as its making me crazy. It should be easy, but that doesn't seem to be the case.
I'd like my buttons on my UINavigationBar to be semi-transparent such that they allow the background of whatever is on the UINavigationBar to show through. This effect is seen in many applications, image examples below. You can do this by setting a custom background on the item, which i think is an unacceptable solution because it requires that you prepare images beforehand, and they won't be adaptable for variable buttons etc. They will not look like Apple UI and I don't believe there is a reason to do this either, UIKit is already drawing the background for these buttons, we just need to change it. The correct solution uses the bar items and views generated by Apple's apis.
UIBarButtonItem is not a UIView subclass. When you create one and add it to a UINavigationBar, some code somewhere in the framework draws a view for it. The framework methods seem to resist anything related to allowing transparency of the bar items, such as the tintColor property.
For example, this does NOT work:
UINavigationItem *item = [[UINavigationItem alloc] init];
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:#"SUCKS" style:UIBarButtonItemStyleBordered target:self action:#selector(whatever:)];
editButton.tintColor = [UIColor colorWithWhite:0.4 alpha:0.3];
item.leftBarButtonItem = editButton;
Nothing I do will make UINavigationBar allow semi-transparency for its bar items. I believe at runtime we need to:
Get the image for the bar item
Mask it for transparency
Set the new image on the bar item
But I haven't been able to get the image at runtime or mask it properly. How do you do this?
Create a custom uiview and draw a semi-transparent black rectangle in it and use that view with initWithCustomView.
see
and
Failing that, you may have to use an image (png). e.g. a 1x1 black pixel png with 30% opacity.You could then initWithImage.
EDIT: I have had this second approach working using:
buttonThree = [[UIBarButtonItem alloc] initWithTitle:#" sort button " style:UIBarButtonItemStyleBordered target:self action:#selector(sortMethod)];
UIImage *thebgUIimage = [UIImage imageNamed:#"semi.png"];
[buttonThree setBackgroundImage:thebgUIimage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
This results in a button that has a transparent background image that the navbar background image shows through. However, you would need to create an image with the rounded corners on and so need an image for each button width. Also I found this thread after trying the above
A brilliant hack is to use the UISegmentedControl with a single segment (as a button) and set its tint color. Have a look at http://charles.lescampeurs.org/2011/02/10/tint-color-uibutton-and-uibarbuttonitem. I have personally implemented this. Feel free to ask any questions.
Instead of searching for code and breaking your head, my suggestion is just to have transparent image which has just border similar to button (add shadow if necessary), create a button of custom type, add the transparent background image to it and you can text as you want. From this custom button, create your bar button item accordingly.
If you're targeting for iOS 5, you can set the background image of the button.
[_button setBackgroundImage:#"image" forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
Note that you'll need to set background images for state UIControlSateSelected and again for both control states for barMetrics: UIBarMetricsLandscape, if your application allows landscape orientation.
Note again this is an iOS 5 feature.
I believe your answer is here: http://sebastiancelis.com/2009/12/21/adding-background-image-uinavigationbar/
I have a UIView has some labels and buttons on it.
Next I also have a image which has a square area that is transparent, now
if I create a UIImageView and add this image which has transparent regions I am not able to see the background view (which has buttons and labels) through this transparent image.
If I play with the alpha value that doesn't work as intended which is to see the transparent regions exactly as it would have appeared on the UIView which has the labels and buttons.
UIImage* image = [UIImage imageNamed:#"TI1.jpg"];
UIImageView* imageView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
Also I would be interested to know if there is other way to achieve what I am trying to achieve.
Basically I want to highlight a certain area of the view which has buttons/labels and make the rest of the area greyed out. My idea was to have this UIImageView with transparent regions in image to achieve that.
Thanks
Ankur
Try setting imageView.opaque = NO;
UIImageView inherits from UIView. According to that class's docs:
This property provides a hint to the drawing system as to how it
should treat the view. If set to YES, the drawing system treats the
view as fully opaque, which allows the drawing system to optimize some
drawing operations and improve performance. If set to NO, the drawing
system composites the view normally with other content. The default
value of this property is YES.
Also, not sure that JPG even supports transparency, so try exporting the image as a PNG to ensure you get the results you're looking for.
I know this is a very beginner question, but I'm obviously a beginner. I have already made a my TabBar but I want to set the background of one of the views as a (jpg) I created. I need to add the background in code (not IB) so that I can allow rotation and resizing when the iphone is rotated.
-thanks
You need to use a UIImageView, which is a subclass of UIView. You can create it as follows:
UIImageView *myImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"myImage.jpg"]];
[self.view addSubview:myImage];
[myImage release];
...so here I've created a UIImageView that uses a JPG called 'myImage' (it will automatically resize the view to fit the image), added it to my view controller, and then cleaned up my memory.
You can try this one to set an image as a background for a view programmatically:
view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"imageName.png"]]
Add an instance of UIImageView to your view, and set it's image to an instance of UIImage created using your file.
You can add the image view in IB or in code -- there's nothing about doing it in IB that would prevent you from resizing, rotating, etc.