I have set some images to custom a UIProgressView and give a rounded style to it, but the problem is when the progress value is changed the control is not anymore rounded on iOS 7.
Here is the code that i use:
self.progressBar.progressImage = [UIImage imageNamed:#"prog_progress"];
self.progressBar.trackImage = [UIImage imageNamed:#"prog_track"];
CGAffineTransform transform = CGAffineTransformMakeScale(1.0f, 5.0f);
self.progressBar.transform = transform;
Progress View Changes:
Please add category to UIProgressView as below
- (CGSize)sizeThatFits:(CGSize)size
{
return CGSizeMake(self.frame.size.width, self.frame.size.height);
}
#end
The progressbar has roundet corners. Have a closer look. Due your skaling you are compressing the image along with its corners. Even if setting insets this aproach will never work. Smal numbers will at one point drop below the inset.
There are many aproaches, but given your image aproach:
A simple blue view under an imageview. the image view has your white image with the grey bar. Give the grey area trabsparency, so the blue is visible. Now u can set the blue view to any size without loosing the round corners.
Related
I want to add border for an image. I don't want to add it completely on 4 sides. I want to add border only on particular 3 sides i.e., top,left & bottom. How to do that?
[imageView.layer setBorderColor:[[UIColor clearColor] CGColor]];
This would set the border on 4 sides of the image. But I want it only on 3 sides. How to do that?
You’ll need to add colored subviews on the edges that you want to have borders. See this answer—for the border on the top, follow the same pattern, but use an autoresizingMask of UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin.
Make sure that the UIImageView has its clipsToBounds set to YES. Create a UIView that is the one border width wider than the image. Set the border on that layer to have the desired color. Set the frame of this view to 0,0, imageWidth+border, image.height and set masksToBounds = YES. Add this as a subview to your UIImageView. It should put a border around all but the right hand side.
You could draw a filled CALayer that is 2px higher and 1px wider behind the actual image layer. Set the frame to be 1px above and to the left of your image.
manipulate the view with shadow and this is done easy peasy
_topInfoView.layer.masksToBounds = NO;
_topInfoView.layer.shadowOffset = CGSizeMake(0, 1);
_topInfoView.layer.shadowColor = [[UIColor grayColor]CGColor];
_topInfoView.layer.shadowRadius = 0.27f;
_topInfoView.layer.shadowOpacity = 0.6;
I would like to obtain a UIView like this :
But i don't know, what is the best way to do this ?
Is this the best way: https://stackoverflow.com/a/9214570/1228634 ?
Create the shadow as a transparent layer at a particular size, also create a stretchable image, like this:
UIImage *shadowImage = [UIImage imageNamed:#"shadow.png"];
shadowImage = [shadowImage stretchableImageWithLeftCapWidth:floorf(shadowImage.size.width/2) topCapHeight:floorf(shadowImage.size.height/2)];
Put the image in a UIImageView with contentMode as scale to fit.
Call your view as "sView". You can add the shadow like this:
UIImageView *shadowImgView = [[UIImageView alloc] initWithImage:shadowImage];
shadowImgView.contentMode = UIViewContentModeScaleToFill;
shadowImgView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
shadowImgView.frame = sView.bounds;
[sView shadowImgView];
[shadowImgView release]; // only needed if you aren't using ARC
You can try to use several pictures (as it works for html), but I don't think it is better than your example:
you need 4 small pictures to make a texture for top, left, right and bottom border
you need 4 pictures for your View's corners;
draw to make a border from first 4 images using repeating;
draw corner images.
In my iPhone application I have to show a UIImage with its all four corners curved so that it looks good on the screen. But my image is a rectangular image which cannot be altered at image level as it comes from a third party.
With UIImageView I can set the frame. Using code, how can I modify my UIImage to have rounded corners?
Any clue?
try this way:
UIImageView *imageView = ...;
imageView.layer.cornerRadius = 7; // change this value as per your desire
imageView.clipsToBounds = YES;
UIView *stateView = [getSomeUIView thisOne];
CGRect currentFrame = stateView.frame;
if(currentFrame.size.height == 0.0) {
currentFrame.size = CGSizeMake(260, 60);
}
else {
currentFrame.size = CGSizeMake(260, 0);
}
stateView.frame = currentFrame;
I would expect all the subviews would be hidden when the height of the frame is set to zero however this does not happen (in the iPhone 4.0.1 Simulator).
Any suggestions why or alternatives?
I was planing to later animate the frame so it's a sliding effect. I can not use the y position and move it off screen nor can I create a element to hide it behind since I'm working with a background image and everything on top is transparent/alpha layer.
I've got the same problem. Solved it with clipsToBounds property:
stateView.clipsToBounds = YES
Subviews will only change size if you set their springs and struts to do so.
By default, they are set to "stay the SAME width and height, and stay the same distance from top left corner of the parent view".
You can set the springs/struts in Interface Builder, or in code. e.g.:
aSubView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
Use UIScrollView instead of UIView. UIScrollView is made to hide "overflow" and works perfectly.
Here's how I'm setting the background image and content mode:
[btn setBackgroundImage:[UIImage imageNamed:#"dots_game_horiz_blue.png"]
forState:UIControlStateNormal];
[btn setContentMode:UIViewContentModeCenter];
Is there a reason that the background image would be pixelated? The frame is slightly larger than the image size, and I just want the image to be centered.
I guess a background image is not considered to be "content" by the UIButton, so the content mode did not apply to the background image. Instead, I set the image of the button and it worked just fine:
[btn setImage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"dots_game_horiz_blue" ofType:#"png"]] forState:UIControlStateNormal];
[btn setContentMode:UIViewContentModeCenter];
In your xib / storyboard, in the Attributes inspector, you need to:
Set your UIButton type to Custom
Set your image in the property Image, not Background
Validating step 2, you can see the button frame changed according to the image set. Set the frame as you want it to be, the image will resize itself to fit in
Select Image for the property Edge in order to apply inset to the button image
Enter inset values you need. If you set 5 to Top, Bottom, Left, Right, the image will be centered inside the button, with a border of 5
You can see the changes in the xib / storyboard view, no need to launch a debug.
Tip: Inset is the same for every images of the button (State Config)
You could also implement - (CGRect)backgroundRectForBounds:(CGRect)bounds to control how the background is drawn.
The correct solution is to change the type from System to Custom. Then the background image will honor the content type instead of just centering. In my case I set it to Aspect Fill and apply a 45 degree corner radius to round it. Simple and works perfectly.
I needed to create rounded buttons for avatar photos and found the answers to this question to help but they did not get me all of the way there. I implemented the backgroundRectForBounds method and scaled the image to fit and it works well.
I have the code on GitHub.
https://github.com/brennanMKE/CircleButton
The method is also listed below. It is important to set the background image and not the image for the button which does not work with this method.
- (CGRect)backgroundRectForBounds:(CGRect)bounds {
UIImage *backgroundImage = [self backgroundImageForState:self.state];
if (backgroundImage) {
CGFloat maxWidth = CGRectGetWidth(self.frame);
CGFloat xDelta = maxWidth / backgroundImage.size.width;
CGFloat yDelta = maxWidth / backgroundImage.size.height;
CGFloat delta = xDelta > yDelta ? xDelta : yDelta;
CGFloat x = floorf((self.bounds.size.width - (backgroundImage.size.width * delta)) / 2);
CGFloat y = floorf((self.bounds.size.height - (backgroundImage.size.height * delta)) / 2);
return CGRectMake(x, y, backgroundImage.size.width * delta, backgroundImage.size.height * delta);
}
else {
return [super backgroundRectForBounds:bounds];
}
}