can I add a uiprogressbar to a uibutton - iphone

I want to know if it is possible to subclass UIButton someway to add another component to it such as a preloader or progress indicator.
In pseudo code what I have in mind is something like the following.
UIButtonWithProgress *button =[[UIButtonWithProgress alloc]init]
[button.progressbar setProgress:50%]

In iOS the class for a progress bar is UIProgressView. If you really wanted to add a UIButton to a UIProgressBar (as in the question title). Then you can use the UIView method addSubView: For example:
UIButtonWithProgress* button = [[UIButtonWithProgress alloc] init];
UIProgressView* progressView = [[UIProgressView alloc] init];
[button addSubview:progressView];
progressView.progress = 0.5;
I think what you really want to do is create your own UIControl (so you should subclass that instead). You can override the method drawRect: to use quartz to draw your own progress bar animation, and the touchesBegan:withEvent: to detect the user tapping on your custom control, as well as adding any other methods you wish to control the state of the control (such as the progress level).
Note: In my code example I also give an example of correctly setting the progress of the UIProgressBar, just in case you were unclear.

Related

Can't get to custom rightBarButtonItem

I've got two viewControllers who each need a login button in the top right corner of the navigation bar.
In viewController1#viewDidLoad, I set up the rightBarButtonItem like so (abbreviated):
// set up the login button on the right
UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *loginImage = [UIImage imageNamed:#"btn_login.png"];
[loginButton setBackgroundImage:loginImage forState:UIControlStateNormal];
[loginButton setFrame:CGRectMake(0, 0, loginImage.size.width, loginImage.size.height)];
[loginButton setTag:1111111];
UIBarButtonItem *loginItem = [[UIBarButtonItem alloc] initWithCustomView:loginButton];
self.navigationItem.rightBarButtonItem = loginItem;
[loginItem release];
I tag it so that in viewWillAppear, I can use viewWithTag:1111111 to figure out if it needs to be hidden or visible, based on whether or not the user is logged in. Simple.
((UIButton *)[self.navigationController.view viewWithTag:LOGIN_BUTTON_TAG]).hidden = true;
When viewController2 gets pushed onto the stack, I basically run the same code to set up my rightBarButtonItem, but I give it a different tag (i.e. 222222).
In viewController2#viewWillAppear, when I look for the viewWithTag:222222, it comes back as null, and so I can't hide/show it.
I noticed though that if I use the same tag as I used in viewController1 (1111111), I can get to it.
Why is this? My tags are actually set up at the top of the file as constants, so it seems inelegant to copy the random number from vc1 into vc2 just so I can get this to work. Instead, I'd like to understand why vc2's tag isn't getting applied to the rightBarButtonItem, and why vc1's tag is still preserved even though I'm in a different viewController.
The easiest most scalable solution is to avoid using viewWithTag: like the plague.
Make the UIBarButtonItem an ivar and then you have instant access to it without any ambiguities.
Replace
[loginButton setTag:1111111];
with
self.loginButton = loginButton;
Then to retrieve do this
self.loginButton;
instead of
(UIButton *)[[[self.navigationController visibleViewController] view] viewWithTag:LOGIN_BUTTON_TAG];
I know which one looks more elegant and robust to me
If I understand correctly what you are doing, the first thing I would try is to check, before this line, in both viewcontrollers:
((UIButton *)[self.navigationController.view viewWithTag:LOGIN_BUTTON_TAG]).hidden = true;
to put a
NSLog("View is %#", self.navigationController.view);
to check if you are sending viewWithTag to the correct view.
Not sure about this, but Are you trying to access to a view, controller by an UIViewcontroller, loaded inside an UINavigationcontroller?
In this case I would use:
(UIButton *)[[[self.navigationController visibleViewController] view] viewWithTag:LOGIN_BUTTON_TAG];

UIView.setalpha is not working?

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

How to create a map (UIImage) with buttons on it that animate?

i have a map (just an image) and i want that a user can click on some places on the map. I think i can do that with adding buttons as subviews. But i also want to animate them. So for example want to have a ring form around the link position. And this ring should animate like pulsing or so. How can i do that the best way?
greets Max
Not sure why nobody has tried to answer this but it's not too difficult.
First, create a UIImageView and configure it to animate your desired effect. The UIImageView documentation is very clear on what to do. Something like:
NSArray *imageArray = [NSArray arrayWithObjects:
[UIImage imageNamed:#"frame1.png],
[UIImage imageNamed:#"frame2.png],
...,nil];
UIImageView *animatedImageView = [[UIImageView alloc] initWithFrame:myFrame];
animatedImageView.animatedImages = imageArray;
animatedImageView.userInteractionEnabled = YES;
[self.view addSubView:animatedImageView];
[animatedImageView startAnimating];
You then have to add the code to respond to touch events on the imageView.
Alternatively, you can create a UIButton subclass that displayed an animated UIImageView per above and then use the standard addTarget:action to respond to user actions.

How do you add more than one UIBarButton on UINavigationItem.rightBarButtonItem (or leftBarButtonItem)?

I have tried this approach/hack:
http://blog.blackwhale.at/2009/06/uibuttons-in-uinavigationbar/
The problem is this leaves a faint seam. I tried setting the background image of the nested toolbar to an image I captured of what it should be. That didn't work. The image was not applied. I have also tried using a nested UINavigationBar and that didn't seem to work.
I have seen this done in several iPhone apps. Does anyone know how?
[EDIT] I want the buttons to look like normal UIBarButtonItems and be able to use system styles like UIBarButtonSystemItemAdd, UIBarButtonSystemItemRefresh. The link I provided does this except you can see a faint seam because it is a UIToolbar nested in the navigationbar..
Please don't mention this breaking the Human Interface Guidelines. (We know).
I appreciate you contributing your hacks... thats the only way to do this!
iOS 5.0 now supports multiple buttons. See the iOS documentation for UINavigationItem. Specifically, the following:
Properties:
#property(nonatomic, copy) NSArray *leftBarButtonItems;
#property(nonatomic, copy) NSArray *rightBarButtonItems;
#property BOOL leftItemsSupplementBackButton;
Methods:
- (void)setLeftBarButtonItems:(NSArray *)items animated:(BOOL)animated;
- (void)setRightBarButtonItems:(NSArray *)items animated:(BOOL)animated;
I posted code to add two buttons to the right of the navigationBar. You can set barStyle = -1 instead of subclassing UIToolbar.
To get rid of the background ('seam') of a UIToolbar, create a subclass of UIToolbar and override the (void)drawRect:(CGRect)rect method. Leave that blank and your UIToolbar will no longer have a background.
Just used this in my own project and worked great. Found this in the comments of: http://osmorphis.blogspot.com/2009/05/multiple-buttons-on-navigation-bar.html
UIView *parentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, myWidth, myHeight)];
// make UIView customView1... (UILabel, UIButton, etc.) with desired frame and settings
[parentView addSubview:customView1];
[customView1 release];
// make UIView customView2... (UILabel, UIButton, etc.) with desired frame and settings
[parentView addSubview:customView2];
[customView2 release];
UIBarButtonItem *customBarButtomItem = [[UIBarButtonItem alloc] initWithCustomView:parentView];
[parentView release];
self.navigationItem.rightBarButtonItem = customBarButtomItem;
[customBarButtomItem release];
see uicatalogue example available at apple's site for free...they used uisegmented control to show three buttons in place of right bar button on navigaion bar...
I can't comment but in addition to #iworkinprogress I had to set the UIToolbar background color to clear:
[toolbar setBackgroundColor:[UIColor clearColor]];
This was also found in the comments of http://osmorphis.blogspot.com/2009/05/multiple-buttons-on-navigation-bar.html.
In iOS 4.x the clearColor seems to have no effect on the UIToolbar, whereas overriding its drawRect: did.
I came up with a helper function I'm using all over my project. Basically it checks if there is already a button on the bar and either add the new one or merge it with existing buttons. So you can call the function just once or multiple times:
+ (void)AddButtonToBar:(UIViewController *)controller withImage:(NSString *)imageName withAction:(SEL)action withFrame:(CGRect) frame{
UIButton *newButton =[[UIButton alloc] init];
[newButton setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
newButton.frame = frame;
[newButton addTarget:controller action:action forControlEvents:UIControlEventTouchUpInside];
if ([[controller.navigationItem rightBarButtonItems] count] == 0)
[controller.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:newButton]];
else {
NSMutableArray *existingButtons = [[NSMutableArray alloc] initWithArray:[controller.navigationItem rightBarButtonItems]];
[existingButtons addObject:[[UIBarButtonItem alloc] initWithCustomView:newButton]];
[controller.navigationItem setRightBarButtonItems:(NSArray *)existingButtons];
}
}
Call it from the view controller:
[Helper AddButtonToBar:self withImage:#"imageName.png" withAction:#selector(myAction) withFrame:CGRectMake(0, 0, 24, 24)];

Touch event in UIImageView

How can I determine what UIImageView I touch in the screen? Like for example, I added 10x10 tiled UIImageView in a UIView. Now in touchesBegan, how can I know the image I touch in screen? Should I use hitTest method in this implementation?
Thanks.
I generally find the easiest way is to subclass UIImageView and add my own touch event handlers.
In fact what I have often done is create one subclass of UIImageView who's sole purpose is to allow another class to act as next responder. That way I don't have to subclass it for every situation. That's only worth doing if you need a number of these views and you weren't subclassing them anyway.
this question was asked many times here. try to use search next time. I usually use UIButtons with custom style to do what you want. such a variant gives you standard methods to catch touches without subclassing.
To follow up on the UIButton suggestion, here's a snippet of what I have done for a UIButton that is rendered with a UIImage.
This was for tracking the specific UIButton image type that got touched within a UITableViewCell, and it passes along the row and section in which this UIButton was placed:
UIButton *_infoButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[_infoButton setImage:[UIImage imageNamed:#"Info.png"] forState:UIControlStateNormal];
[_infoButton setTitle:[NSString stringWithFormat:#"%d:%d", indexPath.section, indexPath.row] forState:UIControlStateReserved];
[_infoButton addTarget:self action:#selector(touchedDetailedInfoButton:) forControlEvents:UIControlEventTouchDown];
// ...
[ _infoButton release];
And here's the associated method for recovering the title and doing something interesting with it:
- (void) touchedDetailedInfoButton:(NSString *)title {
unsigned int _section, _row;
const char * _indexPathCharPtr = [title cStringUsingEncoding:NSUTF8StringEncoding];
sscanf(_indexPathCharPtr, "%d:%d", &_section, &_row);
NSUInteger _path[2] = {_section, _row};
NSIndexPath *_touchedIndexPath = [[NSIndexPath alloc] initWithIndexes:_path length:2];
[self doSomethingInterestingWithIndexPath:_touchedIndexPath];
[_touchedIndexPath release];
}
There's probably an easier way to go about this, so hopefully someone else comes along and offers an alternative (other than subclassing UIImageView, which is a perfectly valid option, as well).