How to make a UIActivityIndicatorView in a UIToolbar tappable - objective-c

I'm using this code to insert an UIActivityIndicatorView to my toolbar
-(void)addActivityIndicatorToToolbar {
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
activityIndicator.userInteractionEnabled = YES;
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
[activityIndicator startAnimating];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
barButton.target = self;
barButton.action = #selector(playButtonPressed:);
NSMutableArray *toolbarItemsMutable = [self.toolbar.items mutableCopy];
[toolbarItemsMutable replaceObjectAtIndex:0 withObject:barButton];
self.toolbar.items = toolbarItemsMutable;
}
However, when I tap the UIActivityIndicatorView the action (playButtonPressed) is not performed.
How can I correct this?

It seems more likely that you want a button with an activity indicator inside it. You can do this by creating a button with a custom view as described in this post. Then you can set the action of this button as normal, and you'll probably want to retain a reference to the activity indicator to start and stop it.

I ended up implementing a poor man's solution by adding an extra view on top of the activityIndicatorView with a gestureRecognizer.

This is a quite old question but why don't you directly add a UITapGestureRecognizer instance to your UIActivityIndicatorView instance ? (works fine on iOS 8.2, I didn't test yet on previous versions).

Related

Adding Bar Button Item

problem
I cannot for the life of me get it to show.
Here's the implementation of the nav bar
_navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
[_navBar setDelegate:self];
[self.view addSubview:_navBar];
_navBar is a UINavigationBar property.
Here's where I add the button.
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(flipView:)];
self.navigationItem.rightBarButtonItem = button;
However, nothing shows. Any help?
self.navigationItem is read automatically by a parent UINavigationController. But you don't have one.
Thus, you have two choices:
Instead of a loose nav bar that you create and put into the interface yourself (as you are doing in your first code), be the child of a UINavigationController. Now setting self.navigationItem will work.
Or, create a navigation item, configure it, and push it manually onto your loose nav bar. Your code will then have this structure:
UINavigationItem* ni = [[UINavigationItem alloc] initWithTitle:// ...];
UIBarButtonItem* b = // ...;
ni.rightBarButtonItem = b;
_navbar.items = #[ni];

How to disable Preview screen when using UIImagePickerController Camera

In iOS7 the build-in camera app doesn't go into the preview screen after taking a picture, is there any way that the UIImagePicker can behave like that.
UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.cameraFlashMode = UIImagePickerControllerCameraFlashModeOff;
I know another solution is to create a custom camera using the AVFoundation class, but that is beyond my knowledge at this point and I really like the looks of the default camera.
Update
So after some more research I found out I could create my own shutter button and set it as a camera overlay. Here is what I did
UIButton *shutter = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[shutter addTarget:self action:#selector(shootPicture) forControlEvents:UIControlEventTouchUpInside];
[shutter setTitle:#"S" forState:UIControlStateNormal];
shutter.frame = CGRectMake(50, 50, 60, 60);
UIView *layoutView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
layoutView.opaque = NO;
layoutView.backgroundColor = [UIColor clearColor];
[layoutView addSubview:shutter];
For the 'shootPicture' method
- (void) shootPicture
{
[picker takePicture];
[self imagePickerController:self.camCtl didFinishPickingMediaWithInfo:nil];
}
If I just have the picker call 'takePicture' I will still get the preview, so instead I forced the picker to call didFinishPickingMediaWithInfo right after taking picture. The result is I don't see the preview screen HOWEVER I don't see the picture either. I know I put 'nil' in didFinishPickingMediaWithInfo because I don't know what to put in at this point. I also know it took a picture is in cache somewhere but I really have no idea how to get it.
Any idea would be greatly appreciated =)
This solution works for me:
self.pickerController.allowsEditing = false

Method called if UIBarButtonItem:initWithImage: is used but not UIBarButtonItem:initWithCustomView:

I have a method that I want to get called when the user clicks on a button in the navigation bar. If I add the button like this then my method gets called:
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"myEditButton"]
style:UIBarButtonItemStyleBordered
target:self
action: #selector(enterEditMode:)];
editButton.title = #"Edit";
[self.navigationItem setRightBarButtonItem:editButton animated:YES];
When the user clicks on the button then my enterEditMode: method gets called.
However using this code the result looks like as in the attachment - there's no text but worse my image is lying on top of a blue button. I can't use the standard system edit button because there is a requirement for it to be colored black not blue and AFAIA I'm unable to change the color of the standard edit bar button to black?
So in a xib I created a parent UIView which contains a UIImageView which is the button image and a UILabel for the text.
Then I create the button item like this:
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithCustomView:self.editButtonParentView];
[editButton setTarget:self];
[editButton setAction: #selector(enterEditMode:)];
editButton.style = UIBarButtonItemStyleBordered;
editButton.target = self;
self.editButtonLabel.text = #"Edit"
[self.navigationItem setRightBarButtonItem:editButton animated:YES];
Where editButtonParentView is an IBOutlet to the partent view in the nib.
This displays perfectly, however the problem is that enterEditMode: does not get called when the user clicks on it.
Why does it not get called?
Thanks
This link is where I found out how you can get the same look as the system 'Edit' button but get any background color you want. Try this code:
// From:
#import "UIBarButtomItem+Tinted.h"
#implementation UIBarButtonItem (Tinted)
+ (UIBarButtonItem *)newBarButtonItemWithTint:(UIColor*)color andTitle:(NSString*)itemTitle andTarget:(id)theTarget andSelector:(SEL)selector
{
UISegmentedControl *button = [[UISegmentedControl alloc] initWithItems:#[itemTitle]];
button.momentary = YES;
button.segmentedControlStyle = UISegmentedControlStyleBar;
button.tintColor = color;
[button addTarget:theTarget action:selector forControlEvents:UIControlEventValueChanged];
return [[UIBarButtonItem alloc] initWithCustomView:button];
}
#end

Adding new view into another subview

I am learning iOS. I m using Xcode 4.3.2 , and i have created a button, and in the callback, i have used the following code.
1) Created single view application
2) creating a new view -> newV
3) creating a button but;
4) adding but as a subview of newV.
5) adding newV as a subview of main view.
-(IBAction) submitButtonPressed:(id)sender
{
NSLog (#" Submit Button is pressed ");
UIView *newV = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
newV.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
UIButton *but = [[UIButton alloc] init];
[but setTitle:#"SubView" forState:UIControlStateNormal];
[newV addSubview:but];
[self.view addSubview:newV];
}
But the problem i get is, only the view viewV is shown, but button is not shown. How to resolve this issue ?
When you created the button, you didn't provide a frame. Try using:
UIButton* but = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, buttonWidth, buttonHeight)];
When you created the button, it was of zero size, so you couldn't see it.
Try adding the button after adding in the view newV.

applicationMusicPlayer volume notification

I am using an applicationMusicPlayer and when i try to change the volume appear the visual notification, as shown in the picture.
Here the code I am using:
[MPMusicPlayerController applicationMusicPlayer] setVolume:newVolune];
Anyone knows how to hide this notification?
I don't know where the docs says so, but if you add a MPVolumeView view to your app the system volume overlay goes away. Even if it is not visible:
- (void) viewDidLoad
{
[super viewDidLoad];
MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame: CGRectZero];
[self.view addSubview: volumeView];
[volumeView release];
...
}
You can use the hardware volume buttons, the setVolume method or directly interact with the control (if visible) that the overlay doesn't show up.
For iOS6 I had to set an image with alpha 0 and non-zero size to the MPVolumeView's image fields in order to get the default volume change notification to disappear.
// hide the hardware volume slider
UIImage *thumb = [[UIImage alloc] initWithCIImage:[UIImage imageNamed:#"volumeHider"].CIImage scale:0.0 orientation:UIImageOrientationUp];
MPVolumeView *hwVolume = [[MPVolumeView alloc] initWithFrame:self.frame];
[hwVolume setUserInteractionEnabled:NO];
hwVolume.showsRouteButton = NO;
[hwVolume setVolumeThumbImage:thumb forState:UIControlStateNormal];
[hwVolume setMinimumVolumeSliderImage:thumb forState:UIControlStateNormal];
[hwVolume setMaximumVolumeSliderImage:thumb forState:UIControlStateNormal];
[self addSubview:hwVolume];
This made the MPVolumeView be "visible" on the screen, but invisible to the user.
I encountered the same issue recently. Instead of adding the MPVolumeView to current view controller's view, I add it to the application's window once at the start of the app:
CGRect rect = CGRectMake(-500, -500, 0, 0);
MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:rect];
[self.window addSubview:volumeView];
This works in both iOS 7 and 8.
Swift 3
You can hide the System MPVolumeView using
override func viewDidLoad() {
super.viewDidLoad()
let volumeView = MPVolumeView(frame: CGRect.zero)
self.view.addSubview(volumeView)
}
I had success with this in iOS 6. Although it wouldn't perform well. It caused quite a bit of lag when sliding the thumbImage. I did have to take out the last 2 lines of code in order for this to work.
[volumeView release];
...
For me, on iOS 7, none of above solutions worked. Here is how I did it:
_volume = [[MPVolumeView alloc] initWithFrame: CGRectMake(-100,-100,16,16)];
_volume.showsRouteButton = NO;
_volume.userInteractionEnabled = NO;
[self.view addSubview:_volume];
[_volume release];
That is, simply set MPVolumeView's frame to an off-screen location such as (-100,-100).