iOS7 - Navigation Controller - Custom Back Button - objective-c

I need to use custom Back button image for my new iOS 7 app. When I use Storyboard and add image by using Attributes inspector, this is what XCode renders for me:
Does anyone know why the Navigation controller behaves this way? I'm regularly putting #2x PNG image into Bar Button Item(under Navigation Item) in my Table View.
#interface MEDevicesViewController : UITableViewController
In my application, I use ECSlidingViewController and I have Table View within Navigation Controller. This is on my navigation controller.
Any help appreciated...

UIButton * button= [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0, 0, 51, 31)];
[button setImage:[UIImage imageNamed:#"back_btn.png"]forState:UIControlStateNormal];
[button addTarget:self
action:#selector(backbtnpressed:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = barButton;
-(IBAction)backbtnpressed:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}

Related

TVOS: UIButton action not working in subview

in the TVOS Application storyboard mainview I added a subview.
Within the subview there are 3 button created in the very same way (the code here belongs to the subview)
UIButton *b = [UIButton buttonWithType:UIButtonTypeSystem];
b.frame = CGRectMake(left, top, 400, size);
[b setTitle:#"Click Me" forState:UIControlStateNormal];
[b addTarget:self action:#selector(Action_Up) forControlEvents:UIControlEventPrimaryActionTriggered];
[self.view addSubview:b];
...
-(void)Action_Up {
[self ShowData];
}
in the simulator and on the real device I can select the button and press it, but the action is never fired.
Could you help ?
Try this.. it worked for me
button.addTarget(self, action: "someMethodName:", forControlEvents: .PrimaryActionTriggered)

add an action on a button without ibaction

I'm working on a mapkit view, i added a button on an annotation like this:
UIButton *bt = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[bt setFrame:CGRectMake(0, 0, 30, 30)];
annView.rightCalloutAccessoryView = bt;
i want to go to another view (for example expleViewController) when touching that button, i'm accustomed to do it using an IBAction method and link it with the button using Interface Builder, but this button is created manually, so how can I link it?
[bt addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
- (void)buttonPressed:(UIButton *)b
{
// do whatever you want ;)
}

Capture dynamic button tap ios

I have button created programmatically in the UIImageView. This view was also created programmatically. When image in this view is tapped the button is created, now I want to capture when this button is tapped but I'm unable to here is my relevant code :
- (void)imageTapped:(UIGestureRecognizer *)sender{
MyImageView *myView = (MyImageView *)sender.view;
NSLog(#"Image tapped is => %#", myView.currentImageName);
//add button to view
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(pressedPlay:)
forControlEvents:UIControlEventTouchDown];
//button image
UIImage *backgroundImage = [UIImage imageNamed:[NSString stringWithFormat:#"playbutton.png"]];
button.frame = CGRectMake(85.0, 175.0, backgroundImage.size.width, backgroundImage.size.height);
myView.userInteractionEnabled = YES;
button.userInteractionEnabled = YES;
[button setImage:backgroundImage forState:(UIControlStateNormal)];
[myView addSubview:button];
}
Now this code should handle the tap but for some reason it doesn't :
- (void)pressedPlay:(UIGestureRecognizer *)sender{
//MyImageView *senderView = (MyImageView *) sender.view;
NSLog(#"%#", #"I Presed button");
}
Any reason why this is not working?
Your code suggests that you are using a UIGestureRecognizer (most likely a UITapGestureRecognizer) to detect the initial tap. The problem is that UIGestureRecognizer is being greedy and stopping the UIButton from receiving the touch.
You will need to do some shifting to disable the gesture recognizer after the initial detection and then reenable it after the play has been tapped.
UIGestureRecognizer has the property
#property(nonatomic, getter=isEnabled) BOOL enabled
or you can choose to implement UIGestureRecognizerDelegate and provide an implementation of
gestureRecognizer:shouldReceiveTouch:
a very simple attempt could look like:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
{
return ![touch.view isKindOfClass:[UIButton class]];
}
and don't forget to set the delegate on the gesture recognizer
myGesture.delegate = self;
set the button frame from:
button.frame = CGRectMake(85.0, 175.0, backgroundImage.size.width, backgroundImage.size.height);
to:
button.frame = CGRectMake(0,0, backgroundImage.size.width, backgroundImage.size.height);
it looks like you are setting button frame outside the imageview's frame and hence the button is not actually getting any interaction.
The Button frame is likely setting the button outside the bounds of the UIImageView, frames are always relative to the superviews coordinate in this case the UIImageView.
[button setExclusiveTouch:YES];

How to avoid corrupted borders when presenting a modal controller in a UIPopOverController

I'm having an issue when presenting a modal view controller within a popover using the modal presentation style UIModalPresentationCurrentContext.
Something strange is happening to the border of the popover, here are some before/after screenshots:
Before presenting the modal controller:
And now once the modal controller has been displayed:
See how the border width on the left and top has changed? When the modal controller is dismissed the border width stays corrupted but if the whole popover is dismissed it displays correctly until the modal controller is shown again.
Here is the code we're using in a sample project that we've created to demonstrate the problem:
- (IBAction)showModalViewButtonTapped:(id)sender
{
UIViewController *viewController = [[UIViewController alloc] init];
UIButton *dismissButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
dismissButton.frame = CGRectMake(30, 30, 100, 25);
[dismissButton setTitle:#"Dismiss" forState:UIControlStateNormal];
[dismissButton addTarget:self action:#selector(dismissButtonTapped) forControlEvents:UIControlEventTouchUpInside];
[viewController.view addSubview:dismissButton];
viewController.view.backgroundColor = [UIColor redColor];
viewController.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentModalViewController:viewController animated:YES];
[viewController release];
}
Has anyone seen this before? any ideas?

UIButton like in UIActionSheet

I would like put UIButton on my UIViewController with style like have buttons in UIActionSheet. What should i add here:
UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];
[ button setFrame: CGRectMake(10, 10, 50, 35) ];
[button setTitle:#"Button from UIActionSheet" forState: UIControlStateNormal];
[self addSubview: button];
You need to get the background image from somewhere and then set it using: - (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state
I believe the .psd linked here has the action button background image http://media.photobucket.com/image/uiactionsheet%20button%20psd/visionwidget/iphone-gui-psd-file.jpg
you can using segmented control - very good trick
segmented = [[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Info",#"Configuration", nil]] autorelease];
[segmented addTarget:self action:#selector(changeView:) forControlEvents:UIControlEventValueChanged];
segmented.tintColor = [UIColor colorWithRed:0.20 green:0.20 blue:0.52 alpha:1.0];
segmented.segmentedControlStyle = UISegmentedControlStyleBar;
Maybe exists some simple way like set some property?
Here is a post on using Apple's private UIGlassButton class.
You can't submit an app like this to the app store, but you can capture the generated background image to reuse in your published apps.