airplay button for UIImages - iphone

Airplay button is showing on the UIToolbar but when clicking it nothing is happening. It should open up airplay picker. but it is not.
UIButton *volumeView = [UIButton buttonWithType:UIButtonTypeCustom];
[volumeView addTarget:self action:#selector(MPVolumeView:) forControlEvents:UIControlEventTouchUpInside];
volumeView.frame = CGRectMake(0, 0, 40, 40);
UIImage *icon = [UIImage imageNamed:#"airplay-1.png"];
[volumeView setImage:icon forState:UIControlStateNormal];
UIBarButtonItem *volumeview = [[UIBarButtonItem alloc] initWithCustomView:volumeView];
-(void)MPVolumeView:(id)sender
{
MPVolumeView *volumeView = [[MPVolumeView alloc] init];
[volumeView setShowsVolumeSlider:NO];
[volumeView setShowsRouteButton:YES];
// [volumeView sizeToFit];
[volumeView release];
}
I dont see anything wrong with the code but still wondering why it is not opening up airplay picker for scrollview images in the app. Actually in this case airplay is for uiimages. can we airplay UIImages.
Thanks for help.

Creating an instance of MPVolumeView doesn't actually open the Airplay picker, it creates the button which upon press, opens the picker.
Try something like this:
MPVolumeView *myVolumeView = [[MPVolumeView alloc] initWithFrame:CGRectMake(0, 0, 45, 33)];
myVolumeView.showsVolumeSlider = NO;
myVolumeView.showsRouteButton = YES;
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:myVolumeView];

Related

How to add a UIBarButtonItem of custom width to a UINavigationItem programmatically without using an image

I'm using iOS 6.1 and want to add a UIBarButtonItem of custom width with title "Info" to the UINavigationItem programmatically when the view loads. I don't want to use an image to achieve this.
I read several threads about how to change the width of a UIBarButtonItem and none seem to work for me.
I tried this but the button does not appear:
- (void)viewDidLoad
{
UIView *infoButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 44)];
UIBarButtonItem *infoButton2 = [[UIBarButtonItem alloc] initWithCustomView:infoButtonView];
[infoButton2 setStyle:UIBarButtonItemStyleBordered];
[infoButton2 setTitle:#"Info"];
self.navigationItem.rightBarButtonItem = infoButton2;
}
What's wrong with this code?
//This worked for me
UIButton *searchButton =[[UIButton alloc]init];
searchButton.frame=CGRectMake(0, 0, 200, 40);
[searchButton setTitle:#"Info" forState:UIControlStateNormal];
searchButton.backgroundColor= [UIColor greenColor];
[searchButton addTarget:self action:#selector(onFilter) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *searchBarButton=[[UIBarButtonItem alloc] initWithCustomView:searchButton] ;
self.navigationItem.rightBarButtonItem = searchBarButton;
you can also add button just like this..
- (void)viewDidLoad
{
UIView *infoButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 50, 44)];
[infoButtonView setBackgroundColor:[UIColor clearColor]];
UIButton *button1 = [[UIButton alloc]initWithFrame:CGRectMake(0,15,45,21)];
[button1 setTitle:#"Info" forState:UIControlStateNormal];
button1.backgroundColor=[UIColor redColor];
[infoButtonView addSubview:button1];
UIBarButtonItem *infoButton2 = [[UIBarButtonItem alloc] initWithCustomView:infoButtonView];
self.navigationItem.rightBarButtonItem = infoButton2;
}

Adding activity indicator to a custom button not working?

I have an application in which i need to add activity indicators above the buttons whenever it has been clicked.I made all the custom buttons like this and add it to navigation bar.`
UIButton *btnNext1 = [UIButton buttonWithType:UIButtonTypeCustom];
btnNext1.frame = CGRectMake(100, 100,38, 38);
UIImage *image = [UIImage imageNamed:#"default.png"];
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = image;
imageView.frame = CGRectMake(10,5,38,38);
[imageView.layer setCornerRadius:5.0];// position it to the middle
[btnNext1 setBackgroundImage:imageView.image forState:UIControlStateNormal];
[imageView release];
[btnNext1 addTarget:self action:#selector(backButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btnNext =[[UIBarButtonItem alloc] initWithCustomView:btnNext1];
self.navigationItem.leftBarButtonItem = btnNext;
[btnNext release];
then in the sender methode i tried
UIButton * button = (UIButton *)sender;
UIActivityIndicatorView *myIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
// Position the spinner
[myIndicator setCenter:CGPointMake(button.frame.size.width / 2,button.frame.size.height /2)];
// Add to button
[button addSubview:myIndicator];
// Start the animation
[myIndicator startAnimating];
But it is not showing up. Can anybody point me where I am going wrong?
Try this in button action
UIButton * button = (UIButton *)sender;
UIActivityIndicatorView *myIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[myIndicator setCenter:button.center];
[button addSubview:myIndicator];
[button bringSubviewToFront:myIndicator];
[myIndicator startAnimating];
I tried with your code it working fine may be the image and indicator both are same color or you are calling different method ,otherwise it's working fine.
once check below images.
before and after button actions
Change your sender method to
UIButton * button = (UIButton *)sender;
UIActivityIndicatorView *myIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
// Position the spinner
[myIndicator setCenter:CGPointMake(button.frame.size.width / 2,button.frame.size.height /2)];
// Add to button
[self addSubview:myIndicator];
// Start the animation
[myIndicator startAnimating];
You need to add indicator to uiview rather than button.

UISearchBar with UINavigationController

I added a UISearchBar and UISearchDisplayController with a UINavigationController. Because we needed a few buttons in the navigationBar, I created a custom view that contains a few buttons, and put that as the rightBarButtonItem. I notice when I click on the search bar, the keyboard pops up from the bottom, and also moves the UINavigationController's navigationBar off the screen. So now I cannot see what I enter in my UISearchBar. Is there a way to get around this? I've seen other apps that it looks like they use a UINavigationBar + multiple buttons on the LHS or RHS of the navigationBar, so I'm assuming this is possible. Thanks.
UIButton *homeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
UIImage *house = [UIImage imageNamed:#"icon_house.png"];
[homeButton setImage:house forState:UIControlStateNormal];
[homeButton addTarget:self action:#selector(homePressed:) forControlEvents:UIControlEventTouchUpInside];
UIButton *filterButton = [[UIButton alloc] initWithFrame:CGRectMake(49, 0, 44, 44)];
[filterButton setTitle:#"Filter" forState:UIControlStateNormal];
[filterButton addTarget:self action:#selector(FilterButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
self.SearchEntry = [[[UISearchBar alloc] initWithFrame:CGRectMake(98, 0, 150, 44)] autorelease];
self.SearchEntry.delegate = self;
UISearchDisplayController *searchDisplayCtlr = [[UISearchDisplayController alloc] initWithSearchBar:_searchEntry contentsController:self];
searchDisplayCtlr.searchResultsDataSource = self;
searchDisplayCtlr.searchResultsDelegate = self;
searchDisplayCtlr.delegate = self;
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 248, 44)];
[containerView addSubview:homeButton];
[containerView addSubview:filterButton];
[containerView addSubview:_searchEntry];
Try to adjust the contentSize of the UIScrollView. Also please take care of the Hierarchy of the controls you have taken. Dont keep UINavigationBar and UISearchView in ScrollView, then it will not move and remains fixed in their position

Can't get programmatically made UIButton to work - iPhone

Here is the code I'm using:
-(void)viewDidLoad
{
NSString *checkerPath = [[NSBundle mainBundle] pathForResource:#"black-yellow-checker" ofType:#"png"];
UIImage *checkerImage = [[UIImage alloc] initWithContentsOfFile:checkerPath];
checkerView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 230.0f, 320.0f, 280.0f)];
[checkerView setImage:checkerImage];
UIButton *backButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
backButton.frame = CGRectMake(45.0f, 175.0f, 230.0f, 50.0f);
[backButton setBackgroundImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"yellowButton" ofType:#"png"]] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(goBackHome:) forControlEvents:UIControlEventTouchDown];
[backButton setTitle:#"Back" forState:UIControlStateNormal];
backButton.titleLabel.font = [UIFont fontWithName:#"Marker Felt" size:30];
backButton.titleLabel.textColor = [UIColor blackColor];
[checkerView addSubview:backButton];
}
- (void)goBackHome:(id)sender
{
NSLog(#"go back pressed");
}
Not sure why this doesn't work. The button shows up, but when I press it nothing happens. It doesn't even change to the "indented" image when I touch it. Nothing. Can't use IB, has to be programmatically. Any thoughts?
UIImageView has userInteractionEnabled set to NO by default. This prevents any subviews from getting touches.
You could either enable user interaction of the image view by
checkerView.userInteractionEnabled = YES;
or create a new UIView to include both checkerView and the backButton.
UIView* newView = ...
...
[newView addSubview:checkerView];
[newView addSubview:backButton];
I believe that the forControlEvents: has to be UIControlEventTouchUpInside.

customView on left button of UINavigationBar

I'm trying to implement UINavigationBar with custom controls. So, I've added UIView on left side of UINavigationBar and trying to add controls to that UIView with following code:
UIView *view = navigationController.navigationItem.leftBarButtonItem.customView;
UIButton *button = [[UIButton alloc] initWithFrame:view.frame];
[button setTitle:#"TEST" forState:UIControlStateNormal];
[view addSubview:button];
[button sizeToFit];
[button release];
Code works, but button doesn't appear.
Any help would be appreciated.
Thank you.
UPD
OK, I gave a try to code below and made such thing:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 50)];
[button setTitle:#"XXX" forState:UIControlStateNormal];
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:button];
navigationController.visibleViewController.navigationItem.leftBarButtonItem = customItem;
[customItem release];
[button release];
The "XXX" title did appear, but it looks like simple label, not button. Any ideas?
Yes, your UIButton doesn't come with any shading. What I've done is used a UISegmentedControl to get shading for free:
// Add the Menu button to the navigation bar
NSString* menuLabel = "ShadeButton";
CGSize textSize = [menuLabel sizeWithFont:[UIFont systemFontOfSize:15.0]];
UISegmentedControl* menuSegmentedButton = [[UISegmentedControl alloc]
initWithFrame:CGRectMake(0.0f, 0.0f, textSize.width, 29.0f)];
menuSegmentedButton.momentary = YES;
menuSegmentedButton.selected = NO;
menuSegmentedButton.segmentedControlStyle = UISegmentedControlStyleBar;
[menuSegmentedButton insertSegmentWithTitle:menuLabel atIndex:0
animated:NO];
[menuSegmentedButton addTarget:self action:#selector(doMenu)
forControlEvents:UIControlEventValueChanged];
UIBarButtonItem* barButton = [[UIBarButtonItem alloc]
initWithCustomView:menuSegmentedButton];
[menuSegmentedButton release];
self.navigationItem.rightBarButtonItem = barButton;
Create your UIBarButtonItem with the UISegmentedControl as shown above rather than a UIButton and you should get the effect you're after. The alternative is to do more work on the button and create a texture/custom image for it yourself.
Note quite right.
If you just want a different title:
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithTitle:#"TEST" style:UIBarButtonItemStylePlain target:nil action:nil];
navigationController.navigationItem.leftBarButtonItem = customItem;
[customItem release];
If you really want a custom view:
// Create Custom View called myView.
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:myView];
navigationController.navigationItem.leftBarButtonItem = customItem;
[customItem release];