iPhone - initWithFrame not working for UIActivityIndicatorView? - iphone

I am using a tableview with custom cells and want to change the content of the tableview on a button click. I am using an activity indicator while the cells are loading data.
This is how I am creating the indicatorview
indicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(141.0, 190.0, 37.0, 37.0)];
indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
indicator.hidesWhenStopped = YES;
[[self tableView] addSubview:indicator];
But it appears as if the frame has no effect on the indicator. The indicator instead of displaying in the center of the table, appears in the navigation bar on the top left corner of the view.
I have no idea what is wrong with this and why this is happening. Can someone please help me out.
alt text http://www.freeimagehosting.net/uploads/95cbe49850.png

initWithFrame: is not the suggested method for creating a UIActivityInidcator. Taking a look at the API, you can see that to create a UIActivityIndicator, you should use
[[UIActivityIndicator alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
Then add it to your view and set its position with setCenter: as suggested by Pablo.
Also, adding it to a location in a table view will make it scroll with the table if the user scrolls up or down. If you put it in the table's superview instead, it will stay in place.

Try this:
activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
[activityIndicator setCenter:CGPointMake(160.0f, 208.0f)];
[activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
[activityIndicator startAnimating];
Calling to setCenter should give the behavior you are expecting.
Good luck!

Related

Add UITextField to cameraOverlay

Hi all I am trying to add a UITextField to my cameraOverlay. I am getting it to show up on my camera view, but it is not editable. Its not responding at all. What approach do I need to take to get it to repsond?
I have looked at this questions, but do not understand how to set a transparent View Controller on top of my cameraOverly.
Suggestion for camera overlay
Thanks in advance!
Steps should be:
Create your picker.
Create an empty view with clearColor background.
Add your textfield with addSubview: to the view in step 2.
Set cameraOverlayView to the view created in step 2.
Present your picker.
In code:
//self.picker and self.textField being your UIImagePickerController and UITextField instances.
emptyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; //This frame will make it fullscreen...
emptyView.backgroundColor = [UIColor clearColor];
[emptyView setAlpha:1.0]; //I think it is not necessary, but it wont hurt to add this line.
self.textField.frame = CGRectMake(100, 100, self.textField.frame.size.width, self.textField.frame.size.height); //Here you can specify the position in this case 100x 100y of your textField preserving the width and height.
[emptyView addSubview:self.textField];
self.picker.cameraOverlayView = emptyView; //Which by the way is not empty any more..
[emptyView release];
[self presentModalViewController:self.picker animated:YES];
[self.picker release];
I typed the code here myself so it could have some typo, Hope it helps!

How do I show a UISearchBar on the right side of a UINavigationItem?

I am trying to display a UISearchBar in place of the button shown on the right side of the UINavigationItem. I am using this code:
- (void)viewDidLoad {
[super viewDidLoad];
UISearchBar *searchBar = [[UISearchBar alloc] init];
UIBarButtonItem *navRight = [[UIBarButtonItem alloc] initWithCustomView:searchBar];
[[self navigationItem] setRightBarButtonItem:navRight];
[searchBar release];
[navRight release];
}
However, the displayed search bar is only a few pixels wide. I need to make it wider.
Am I going about this the wrong way? Is it at all possible?
You need to set the UISearchBar's frame at some point.
e.x.
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];
EDIT: Regarding the button-esque artifact under the UISearchBar
The button-y thing that you see under the UISearchBar is actually a background element of the UISearchBar, not the result of the search bar overlapping a UIBarButtonItem. Since there doesn't seem to be a nice way to hide this (and I hope someone can come along and correct me), I can only point you in the direction of a hack that comes with the usual perils of SDK version dependence, inelegance, and potential for more headaches.
EDIT 2: Another potential solution
You could also set your UISearchBar's frame's height to 44 (the height of the navbar) and then the background should blend nicely with the navbar. Based off this SO post I recently saw.

On clicking the button to changed the view / Is need to use synchronous method in iPhone?

I have created scroll view and set the buttons in the scroll view. The buttons are scrolling horizontally. I created table view as subview for view controller. On clicking the buttons, the datas are displaying in the table view from RSS feeds using XML Parsing. SO changing the datas in the table view which depends the button clicking. When i changed to select the next button, the parsing will be started and displayed the datas. so it takes some times. In the mean time i want to display some view or disable the view(that means,on that parsing time the view is disable or user cant do anything like freeze with activity indicator). On changing the each buttons, the action will be happened. I referred some tutorials, but i cant get any idea? Some people told me to use synchronous method to solved the problem. But i don't have any idea about it. Please guide me and help me out.Give me some sample apps and Links.
see my below image,
Thanks in Advance!
Regards,
Pugal
Don't use synchronous network calls to disable user input. Whoever suggested that gave you very bad advice.
If you just want to disable input for your current view and its subviews, you can do self.view.userInteractionEnabled = NO; in your view controller.
If you want to disable input for the entire window, you can do self.view.window.userInteractionEnabled = NO;
You won't need to disable user interaction at all if you overlay a full-screen view on top of your user interface. Based on your mock-up image, I think this is what you're trying to do. To do this, you can do something like this:
self.overlayView = [[[UIView alloc] initWithFrame:self.view.window.bounds] autorelease];
self.overlayView.backgroundColor = [UIColor blackColor];
self.overlayView.alpha = 0.5f;
[self.view.window addSubview:self.overlayView];
self.activityIndicator = [[[UIActivityIndicator alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
self.activityIndicator.center = self.view.window.center;
[self.view.window addSubview:self.activityIndicator];
[self.activityIndicator startAnimating];
self.activityLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
self.activityLabel.text = #"Loading...";
[self.activityLabel sizeToFit];
self.activityLabel.center = CGPointMake(self.activityIndicator.center.x, self.activityIndicator.center.y - self.activityIndicator.frame.size.height);
[self.view.window addSubview:self.activityLabel];

How to add Activity Indicator as subview to UIButton?

Can we add activity indicator as subview to UIButton?
If yes then plz tell me how to do that?
I used [button addSubview:activityIndicator];
Not working...
I found that adding a UIActivityIndicatorView to a UIButton was a really useful method to allow users to know something is happening without having to use the MBProgressHUD (I think the HUD is really good but should not be used in all situations.
For this reason I created two functions:
I have already allocated my UIButton so it is a class variable called _confirmChangesButton
I then create my activity indicator, set its frame (taking into account the button size) and then adding the indicator is easy.
- (void)addActivityIndicatorToConfirmButton {
// Indicator needs to be in the middle of the button. So half the screen less half the buttons left inset less half the activity indicator size
CGRect rect = CGRectMake([UIScreen mainScreen].bounds.size.width/2 - 10 - 15, 5, 30, 30);
UIActivityIndicatorView * activity = [[UIActivityIndicatorView alloc] initWithFrame:rect];
activity.hidesWhenStopped = YES;
[_confirmChangesButton setTitle:#"" forState:UIControlStateNormal];
[_confirmChangesButton addSubview:activity];
[activity startAnimating];
}
Having a removal function is also useful if you are using blocks. It might be that the completion task comes back with a failure and so we want to remove the indicator and change the title back. In this function we need to make sure to remove the indicator and not the button label which is the other subview on this button.
- (void)removeActivityIndicatorFromConfirmButton {
UIActivityIndicatorView * activity = _confirmChangesButton.subviews.lastObject;;
[activity removeFromSuperview];
[_confirmChangesButton setTitle:#"Confirm Change" forState:UIControlStateNormal];
}
I found that using these two you can create a much better user experience letting the user know what is going on when they press buttons.
Hope this helps
Use the below code below to add acitivity indicator a button or any uiview object
UIActivityIndicatorView *aView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
aView.frame = CGRectMake(0, 0, {yourButton}.frame.size.width, {yourButton}.frame.size.height);
aView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
[{yourButton} addSubview:aView];
[aView startAnimating];
Hope this will help..
I don't think it's possible to add a view to a button. UIButton have this method because it's inherited from UIVIew.
The real question is : why do you want to add an activity indicator on a button and not elsewhere ?
did you do [activityIndicator startAnimating]; ALso as u are using it in a tableview just check if the tags are set properly

Problem with UIActivityIndicatorView on iPhone

I want to show UIActivityIndicatorView on my iphone when it changing the view.
I have written the following code:
- (void)viewDidLoad
{
spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[spinner setCenter:CGPointMake(320/2.0, 460/2.0)]; // I do this because I'm in landscape mode
[self.view addSubview:spinner];
}
and on button's click event I want to change the view and in between that time I want to show that indicatorView So, I write
-(IBAction)buttonClick:(id)sender
{
[spinner startAnimating];
ViewController *lController = [[ViewController alloc] initWithNibName: #"View" bundle:nil];
self.viewController = lController;
[lController release];
//
[[self mainController] dismissModalViewControllerAnimated:YES];
[lViewController.view removeFromSuperview];
[self.view addSubview: lController.view];
[spinner stopAnimating];
}
It is not displaying the Indicator, So plz tell me where I am wrong?
UIActivityIndicator animates on the main thread (animation frames change per run loop). If you start, execute code, and stop, it never has a chance to animate (since it never exits the current run loop).
Try running your code on a background thread. This will allow the main thread to process animation frames.
In buttonClick it looks like you're adding the lController.view "on top" of the spinner (which was added earlier in viewDidLoad). It's hard to tell from your snippet what's going on with the modal dismissal so let's assume that's not the culprit.
You could try either calling [self.view bringSubviewToFront:spinner] after adding the new subview or else [self.view insertSubview:lController.view belowSubview:spinner] to put your view underneath the spinner. You may also want to set the hidesWhenStopped property on the spinner to YES so it automatically hides when you stop it.
Another thing to keep in mind is that loading and switching views may not actually take that long, so the spinner may not appear if things happen too fast.