Ipad 在iOS8中更改操作表弹出箭头
我正在使用Ipad 在iOS8中更改操作表弹出箭头,ipad,ios8,uipopovercontroller,uiactionsheet,uialertcontroller,Ipad,Ios8,Uipopovercontroller,Uiactionsheet,Uialertcontroller,我正在使用UIAlertController。但在iOS 8的iPad上,操作表显示的是popover arrow。有没有办法把箭藏起来 这是我的密码: UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"this is alert controller" message:@"yeah" preferredStyle:UIAlertControllerStyleActionSheet
UIAlertController
。但在iOS 8的iPad上,操作表显示的是popover arrow。有没有办法把箭藏起来
这是我的密码:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"this is alert controller" message:@"yeah" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel action")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
NSLog(@"Cancel action");
}];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"OK", @"OK action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(@"OK action");
}];
UIAlertAction *deleteAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"Delete", @"Delete action")
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action) {
NSLog(@"Delete action");
}];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[alertController addAction:deleteAction];
UIPopoverPresentationController *popover = alertController.popoverPresentationController;
if (popover) {
popover.sourceView = self.view;
popover.sourceRect = self.view.bounds;
popover.permittedArrowDirections = UIPopoverArrowDirectionUnknown;
}
[self presentViewController:alertController animated:YES completion:nil];
您使用UIPopoverPresentationController显示警报时走错了方向。你只是不需要那些被剪掉的代码
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"this is alert controller" message:@"yeah" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel action")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
NSLog(@"Cancel action");
}];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"OK", @"OK action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(@"OK action");
}];
UIAlertAction *deleteAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"Delete", @"Delete action")
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action) {
NSLog(@"Delete action");
}];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[alertController addAction:deleteAction];
[self presentViewController:alertController animated:YES completion:nil];
解决方案:
使用下一行从行动表中删除箭头
[yourAlertController.popoverPresentationController setPermittedArrowDirections:0];
样本
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Test Action Sheet" message:@"Message" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action)
{
NSLog(@"Cancel action");
}];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:@"Ok"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(@"OK action");
}];
UIAlertAction *otherAction = [UIAlertAction
actionWithTitle:@"Other"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(@"Otheraction");
}];
[alertController addAction:okAction];
[alertController addAction:otherAction];
[alertController addAction:cancelAction];
// Remove arrow from action sheet.
[alertController.popoverPresentationController setPermittedArrowDirections:0];
//For set action sheet to middle of view.
alertController.popoverPresentationController.sourceView = self.view;
alertController.popoverPresentationController.sourceRect = self.view.bounds;
[self presentViewController:alertController animated:YES completion:nil];
输出杰金的回答,用Swift:
popoverController.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
如果您有导航/状态栏,则所选答案不会使警报居中。要使警报控制器精确居中,请执行以下操作:
alertController.popoverPresentationController.sourceRect = [self sourceRectForCenteredAlertController];
alertController.popoverPresentationController.sourceView = self.view;
alertController.popoverPresentationController.permittedArrowDirections = 0;
使用方便的方法:
- (CGRect)sourceRectForCenteredAlertController
{
CGRect sourceRect = CGRectZero;
sourceRect.origin.x = CGRectGetMidX(self.view.bounds)-self.view.frame.origin.x/2.0;
sourceRect.origin.y = CGRectGetMidY(self.view.bounds)-self.view.frame.origin.y/2.0;
return sourceRect;
}
此外,如果旋转视图,警报控制器不会保持居中。要使警报控制器居中,需要在旋转后更新sourceRect。例如:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
// Check if your alert controller is still being presented
if (alertController.presentingViewController) {
alertController.popoverPresentationController.sourceRect = [self sourceRectForCenteredAlertController];
}
}
或者,如果不想使用旋转事件,可以使用popopoverpresentationcontroller
delegate方法重新定位popover:
- (void)popoverPresentationController:(UIPopoverPresentationController *)popoverPresentationController willRepositionPopoverToRect:(inout CGRect *)rect inView:(inout UIView *__autoreleasing _Nonnull *)view
{
// Re-center with new rect
}
使用[alertControllerActionSheet.popoverPresentationController设置许可证行方向:0];rect.orign.x=。。。和.y==。。。不需要。