How to do "actionSheet showFromRect" in iOS 8? - uipopovercontroller

In iOS 7, I show actionSheet by "showFromRect":
[actionSheet showFromRect:rect inView:view animated:YES];
But in iOS 8, this doesn't work. They they replace the implementation and suggest us using UIAlertController. Then how do I show this actionSheet like a popover?

Using UIAlertController you can access the popoverPresentationController property to set the sourceView (aka inView) and sourceRect (aka fromRect). This gives the same appearance as the previous showFromRect:inView:
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Title" message:#"message" preferredStyle:UIAlertControllerStyleActionSheet];
// Set the sourceView.
alert.popoverPresentationController.sourceView = self.mySubView;
// Set the sourceRect.
alert.popoverPresentationController.sourceRect = CGRectMake(50, 50, 10, 10);
// Create and add an Action.
UIAlertAction *anAction = [UIAlertAction actionWithTitle:#"Action Title" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
NSLog(#"Action Pressed");
}];
[alert addAction:anAction];
// Show the Alert.
[self presentViewController:alert animated:YES completion:nil];

I also meet this problem like you, but my case is that I show a UIActionSheet in UIWindow.view on the iPad device, and the UIWindow doesn't set rootViewController.
So, I found, if we show a UIActionSheet in a window whose rootViewController equals to nil, the UIActionSheet could not show out.
My solution is to set
window.rootViewController = [[UIViewController alloc] init];
then,
[actionSheet showFromRect:rect inView:window.rootViewController.view animated:YES].
Hope this will help you!

according to this https://developer.apple.com/library/ios/documentation/Uikit/reference/UIActionSheet_Class/index.html thread
UIActionSheet is deprecated, you should use UIAlertController instead of UIActionSheet.
Thanks.

I found out the point is in iOS 7, you show actionSheet in a new window actually when using "showFromRect: inView:" ;
But in iOS 8 you show the actionSheet just on the view you send in parameters.
So the solution is to send
self.superView
[actionSheet showFromRect:rect inView:[self.superView] animated:YES];
Me and my colleague figured it out by randomly trying.

Related

Custom Alert view controller to select date in IOS [duplicate]

This question already has answers here:
How to add date picker to UIAlertController(UIAlertView) in Swift?
(2 answers)
Closed 5 years ago.
I have a custom alert view controller to select date from date picker, but i'm confuse bit that how can i add two button in alert view controller and when user click the button than it shows the date picker in alert view controller and how can i change the color of alert view controller. My code is ,
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"\n\n\n\n\n\n\n\n\n\n\n" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIDatePicker *picker = [[UIDatePicker alloc] init];
[picker setDatePickerMode:UIDatePickerModeDate];
[alertController.view addSubview:picker];
[alertController addAction:({
UIAlertAction *action = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(#"OK");
NSLog(#"%#",picker.date);
}];
action;
})];
UIPopoverPresentationController *popoverController = alertController.popoverPresentationController;
popoverController.sourceView = sender;
popoverController.sourceRect = [sender bounds];
[self presentViewController:alertController animated:YES completion:nil];
It looks like this,
If I remember well this question as already been asked few time , and each time the answer was "you should create a viewcontroller and use it as an alert" and , seeing what you need to do I also think it's a better way to do, + you can make your controller look like an alert adding a shadow...
See this answer : How to add date picker to UIAlertController(UIAlertView) in Swift?

How can i Change the frame of uialertaction in uialertcontroller in objective c

I am adding the alertView in whole application. and i want to change the size of ok or cancel button in alertView so that i can set the small image of actions.
Anyone please help me for this.
You cannot change the frame of default Alert view for this you have to use custom alert view. Please refer the below links for custom alert views
https://github.com/dogo/SCLAlertView
https://github.com/vikmeup/SCLAlertView-Swift (Swift)
https://github.com/mtonio91/AMSmoothAlert
You can not change the size of ok or cancel button in alertView.
The only solution that I could figure is to make a custom view with UIVisualEffect and show it like UIActionSheet
Welcome if other solution is there :)
if you want to do this for adding image, then try following way:
UIAlertController * view= [UIAlertController
alertControllerWithTitle:#"Add Image"
message:#"Image Added successfully"
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* add = [UIAlertAction
actionWithTitle:#"add"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
//Do some thing here
[view dismissViewControllerAnimated:YES completion:nil];
}];
[add setValue:[[UIImage imageNamed:#"add.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:#"image"];
[view addAction:add];
[self presentViewController:view animated:YES completion:nil];

Show UIAlertView and keyboard simultaneously

It looks like iOS 8 has a bug where alerts with text input do not show the keyboard. I have tried this hack.
The problem with the hack is that first the alert appears, and only afterwards does the keyboard appear. This causes the alert to "jump up" to make space for the keyboard.
How can I have a UIAlertView with text input, where the keyboard appears immediately?
(Note: For an example of what I want, go to Voice Memos, record a new memo, save, and you'll be prompted to enter a name with a UIAlertView with text input. There, the keyboard appears at the same time as the UIAlertView.)
I am not sure this will perfectly solve the problem of keyboard and alertView appearing simultaneously. But I would recommend you to use the newer api. I am posting this as an answer as its difficult to put code into comments.
For some reasons UIAlertView has been deprecated in iOS 8. Instead of using the UIAlertView you should use the UIAlertController with style UIAlertControllerStyleAlert. Present it and then open keyboard.
UIAlertController *alert = [UIAlertController alertControllerWithTitle:yourTitle message:yourMessage preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
Since the alert now is shown as UIViewController the keyboard won't shift the alert box upside.
//ctrl+k to appear keyboard and ios8 keyboard appearing simultaneously issue would solve by this...
UIAlertController * alert = [UIAlertController alertControllerWithTitle:#"" message:#"Registration Successfully" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * ok = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
// [self.view endEditing:YES];
// [self.navigationController popToRootViewControllerAnimated:YES];
}];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];

UIActionSheet presented using Popover causing issues

Issue
I'm having issues with UIAlertController. It's an iPad project, in that I have two classes, say First and Second.
I'm presenting Second from First, using a modal presentation. In Second Class I have a button, clicking on that I'm showing UIActionSheet to perform some actions. It works perfectly except if user clicks on the button rapidly the Second class is being dismissed.
Code
First VC:
- (IBAction)showNext:(id)sender
{
UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"SecondVC"];
[self presentViewController:vc animated:YES completion:nil];
}
Second VC:
- (IBAction)showActionSheet:(UIButton *)sender
{
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
[alertVC addAction:[UIAlertAction actionWithTitle:#"LogOut"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action)
{
}]];
[alertVC addAction:[UIAlertAction actionWithTitle:#"Update"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
}]];
[alertVC setModalPresentationStyle:UIModalPresentationPopover];
UIPopoverPresentationController *popPresenter = [alertVC popoverPresentationController];
popPresenter.sourceView = sender;
popPresenter.sourceRect = sender.bounds;
[self presentViewController:alertVC animated:YES completion:nil];
}
No other code in both classes. I don't know why it happens, Actually it happened to one of my Client project, So I created a test app with above code and it's also causing the issue.
Screen Capture
What I have tried:
I checked UIAlertController Class Reference
Did a search on Google,, nothing came up
Alternatives:
If I set Second class as my rootViewController, the issue won't be there (But I can't do that, I need to navigate back and forth)
If I disable the popover dismissal when touched outside, it can avoid the issue. But I need this workflow.
Can anybody please help me ? I'm totally lost at this moment, not getting any useful info.
Thanks in advance.
I don't know what is the issue, but I fixed it. I used popoverPresentationControllerShouldDismissPopover and returned YES.
- (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController;
{
return YES;
}
Eureka:
It was an accident!!! I didn't get any other way to fix this issue, so I thought will do the second alternative. While writing I accidentally wrote YES instead of NO. But it worked, I don't know how it worked and what is the reason for that issue.

Obj-C, why is my UIActionSheet Cancel button hard to tap?

I've noticed in my app that my cancel button is hard to tap, it seem like the hit area is not in the center.
How do I fix this ?
Heres my code...
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#""
delegate:dg
cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil
otherButtonTitles: #"Help Pages", #"Tutorial",
#"Feedback / Questions ", #"Facebook Group", #"About", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[actionSheet setTag:CommonUIActionSheetHelp];
[actionSheet showInView:vw];
[actionSheet release];
I think your problem lies in the [actionSheet showInView:vw];, perhaps you are using tabBarController/toolBar in you application(at bottom), this error occurs at that time.
you should use either showFromToolbar or showFromTabBar as per your design. If your design is different then please mention it.(if no tabBar/toolBar there).
Thanks
agree whit Ravin answer and you can try
[actionSheet showInView:[UIApplication sharedApplication].keyWindow];
it can help also
I have similar problem in iOS 8, using UIAlertController with style UIAlertControllerStyleActionSheet.
The problem is that the first tap on the action sheet is ignored.
Turned out it got to do with my text field's keyboard interfering with the touches.
The fix is to dismiss the keyboard first, then present the action sheet.
[textfield resignFirstResponder];
[self presentViewController:alert animated:YES completion:nil];

Resources