I'm trying to create a custom UIProgressView and I want it to look like this:
But everything that I'm trying is not working (iOS7). I want it to support iOS5+ and also in iOS7. Finally I did something and I almost there, but I have few problems.
This is how it looks now:
How can I put the image behind the progress view it self, also, how can I change the color of the progress bar it self and how can I force it to stay on the margins of the image?
How can I change the frame of the progress view?
This is my code:
- (void)createProgressView
{
// NOT CHANGING THE FRAME
self.packageUtilizingCellProgressView.frame = CGRectMake(105, 20, 91, 14);
self.packageUtilizingCellProgressView.progressViewStyle = UIProgressViewStyleBar;
// CHANGING THE FRAME
[[UIProgressView appearance] setFrame:CGRectMake(105, 20, 91, 14)];
}
I've also created a subclass of UIProgressView called MYProgressView and in drawRect I've added those lines:
- (void)drawRect:(CGRect)rect
{
UIImageView *background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"progress_view_transparent_background.png"]];
[background.image drawInRect:rect];
[self bringSubviewToFront:background];
}
Please tell me what am I doing wrong and how can I do it the right way?
Thanks in advance!
A solution could be to use a custom Progress View using ImageProgressBar.
Create your custom progress bar and add a UILabel on top of it.
It support iOS5+ and iOS7
Related
I'm asking since the usual answer, modifying the frame in viewDidLayoutSubviews, does not work - unless you can find a mistake in my code. The frame gets set to the correct width and height, but iOS 7 does not respect the frame.
Currently, the app released long ago looks like this and works on iOS 6 and 7:
https://itunes.apple.com/se/app/eksjo/id435475192?mt=8
Recompiling gives this:
https://www.dropbox.com/s/pzyv2vhtlmlxkoe/Photo%202013-12-11%2009%2047%2030.png
-(void)viewWillAppear:(BOOL)animated {
UIImageView *iv=[[UIImageView alloc] initWithFrame:r(320-102/2,0,102,44)];
iv.image=[UIImage imageNamed:#"Eksjologo5bar.png"];
self.navigationItem.titleView=iv;
[iv release];
}
-(void)viewDidLayoutSubviews {
CGRect frame=self.navigationItem.titleView.frame;
frame.size.width=102;
frame.size.height=44;
self.navigationItem.titleView.frame=frame;
}
All I want to do is put a logo image in the center of the Navigation Bar. I'm looking for a minimum code change to the viewWillAppear code to do this and still be compatible with iOS 6.x.
Edit: It may also be an iOS 6 issue and not an iOS 7 issue. If you can explain why it should be done like in this question, it's an answer to my question: My UINavigationitem's TitleView getting expanded in ios 6
Here is what I do
UIImageView *logoImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, desired_image_width, desired_image_height)];
// if you need to resize the image to fit the UIImageView frame
logoImage.contentMode = UIViewContentModeScaleAspectFit;
// no extension name needed for image_name
[logoImage setImage:[UIImage imageNamed:#"image_name"]];
UIView *logoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, logoImage.frame.size.width, logoImage.frame.size.height)];
[logoView addSubview:logoImage];
self.navigationItem.titleView = logoView;
You may notice that I add the UIImageView instance to an UIView instance before setting the navigationItem's titleView. You may set the UIImageView instance to navigationItem's titleView directly, but the logo will be off center when you navigate to the next page and you still want to show the logo with the back button. The navigation bar will automatically put the UIView in the center, but UIImageView (although UIImageView is a subclass of UIView, I just don't know why).
I am trying to use the background view of the image view of a UIButton but for some reason it will not show up. This is what I have tried:
detailCell.greenPriorityButton.imageView.frame = detailCell.greenPriorityButton.frame;
[detailCell.greenPriorityButton.imageView setBackgroundColor:[UIColor greenColor]];
[detailCell.greenPriorityButton.imageView setHidden:NO];
[detailCell.greenPriorityButton.imageView setOpaque:YES];
I have called NSLog on the imageView property and it seems everything is as it should be. I can also tap on the button and the method associated with it will be called so I know it exists. Just in case I am missing something here is the NSLog return:
<UIImageView: 0x1d97e1b0; frame = (254 61; 20 20); clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x1d97e210>>
The reason I am not using dot notation to set the image view properties above is because they don't seem to change the values. For example, if I say detailCell.greenPriorityButton.imageView.hidden = NO; it doesn't seem to do anything.
Thanks for any help!
Edit
The reason for not just setting the background color of the button and not its image view is because I am trying to create a small shape in the button. However I want the tappable space to have margins around the shape so it is still user friendly. I thought the image view property would lend useful as I could manipulate the frame and layer of that separately from the frame and layer of the button.
I have now tried adding a UIView *greenBackground as a subview to the button, however this doesn't appear either.
If you want a view that you can have within the view of the button for the purpose of setting its color then I would think that trying to use the imageview is the wrong approach. You could just add a subview that has the changed background color. something like this:
UIView *colorView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 10, 10)];//Whatever rect you want that will be in reference to the frame of the button
colorView.backgroundColor = [UIColor greenColor];
[detailCell.greenPriorityButton addSubview:colorView];
I have created a bar to appear over the keyboard for next/previous/done like the safari browser. However, the setalpha property of the UIview of the bar doesn't seem to be working. No matter what value I set it to, nothing changes. Here is the code...
here is where the create view is called..
-(void)textFieldDidBeginEditing:(UITextField *)textField{
// Call the createInputAccessoryView method we created earlier.
// By doing that we will prepare the inputAccView.
[self createInputAccessoryView];
// Now add the view as an input accessory view to the selected textfield.
[textField setInputAccessoryView:inputAccView];
// Set the active field. We' ll need that if we want to move properly
// between our textfields.
txtActiveField = textField;
}
here is where i actually create and set its values...
-(void)createInputAccessoryView{
// Create the view that will play the part of the input accessory view.
// Note that the frame width (third value in the CGRectMake method)
// should change accordingly in landscape orientation. But we don’t care
// about that now.
inputAccView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 310.0, 40.0)];
// Set the view’s background color. We’ ll set it here to gray. Use any color you want.
[inputAccView setBackgroundColor:[UIColor darkGrayColor]];
// We can play a little with transparency as well using the Alpha property. Normally
// you can leave it unchanged.
[inputAccView setAlpha: 0.1f];
... code for adding buttons and their properties
}
so basically this is all there is to it. but the setAlpha property does nothing, regardless of what I set it to. However, background color works fine. Any ideas?
Thanks
If you want it to look like the prev/next/done bar in Safari etc, create a UIToolbar with the style set to translucent black. This also has the advantage of laying out your buttons nicely, with the correct style, and auto adjusting between landscape and portrait.
Not a direct answer to your question but probably a better way of acheiving what you actually want.
out of utter curiosity try using:
UIView *overlay = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 310.0, 40.0)];
overlay.backgroundColor = [UIColor blackColor];
overlay.alpha = 0.5f;
[self.view addSubview:overlay];
it's hard to tell what the issue is without being able to see how many views you have and how you've stacked them. check you've defined your "addSubview" and what the hierarchy looks like
UIImageView *navBarImageView = [[UIImageView alloc] initWithImage:navBarImage];
[navBarImageView setFrame:CGRectMake(0, 0, 320, 44)];
self.navigationItem.titleView = navBarImageView;
[navBarImageView release];
I am trying to add an image to my navigationBar, but when I used the code as listed above, it places the image into the navigationBar but leaves a grey gap to the left and right of the image. The image was created at 320x44, I'm not sure why it is resizing it despite the fact that I am setting the frame.
That's... probably not how you want to do that—it's not what the titleView is for. The usual way to do a custom navigation-bar background is to create a category on UINavigationBar, like so:
#implementation UINavigationBar(MyCustomBackground)
- (void)drawRect:(CGRect)r
{
[[UIImage imageNamed:#"my-navigation-background.png"] drawInRect:self.bounds];
}
#end
Throw that in a .m file in your project and you should see all of your navigation bars use "my-navigation-background.png" as their background.
When you try deleting a note in iPhone's Notes application, an UIActionSheet pops up. The sheet is translucent (but not black translucent). How is that achieved? Is it possible to make the background of UIActionSheet a certain color?
I usually implement the following delegate method:
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
Just to make a sample. In this case I use a stretched png as a background:
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
UIImage *theImage = [UIImage imageNamed:#"detail_menu_bg.png"];
theImage = [theImage stretchableImageWithLeftCapWidth:32 topCapHeight:32];
CGSize theSize = actionSheet.frame.size;
// draw the background image and replace layer content
UIGraphicsBeginImageContext(theSize);
[theImage drawInRect:CGRectMake(0, 0, theSize.width, theSize.height)];
theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[[actionSheet layer] setContents:(id)theImage.CGImage];
}
and this is the result:
alt text http://grab.by/4yF1
You can use the code below:
actionSheetObj.actionSheetStyle=UIActionSheetStyleBlackOpaque;
or
actionSheetObj.actionSheetStyle=UIActionSheetStyleBlackTranslucent;
actionSheetObj.actionSheetStyle=UIActionSheetStyleBlackTranslucent;
It's not too difficult. You can use the following code:
CGSize mySize = myActionSheet.bounds.size;
CGRect myRect = CGRectMake(0, 0, mySize.width, mySize.height);
UIImageView *redView = [[[UIImageView alloc] initWithFrame:myRect] autorelease];
[redView setBackgroundColor:[UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.5]];
[myActionSheet insertSubview:redView atIndex:0];
Just make sure you present the UIActionSheet before doing this or the size wont be set. That makes it kind of ugly, but you could do something like:
[myActionSheet showInView:self.view];
if (!redAdded) {
redAdded = YES;
//THE ABOVE CODE GOES HERE
}
You can definitely adjust the opacity by setting the alpha value. Interface Builder lets you edit the value directly, but in code I think you would do:
[[myActionSheet view] setOpaque:NO];
[[myActionSheet view] setAlpha:0.5];
I'm not sure if you need the setOpaque call or not - I think that is used to help optimize performance, as the iPhone won't try to render anything hidden by the opaque view.
It looks black to me (note: using 2.2.1). The only reason there's a color to it is because of the yellow behind it.
One option would be to use the black transparent background and find out the size and speed of the action sheet. Then create a view and animate it in at the same time you show the action sheet, just underneath it, to give it a tint different than the color naturally behind the action sheet. You would have to make the color view also translucent so you could see behind that as well.
I'm not sure if you can, but you might also try adjusting the opacity of the action sheet itself as well.