如何在 iOS 8 中执行“actionSheet showFromRect”?

2023-12-09

在iOS 7中,我通过“showFromRect”显示actionSheet:

[actionSheet showFromRect:rect inView:view animated:YES];

但在 iOS 8 中,这不起作用。他们替换了实现并建议我们使用 UIAlertController。那么我如何像弹出窗口一样显示这个actionSheet呢?


使用 UIAlertController,您可以访问 popoverPresentationController 属性来设置 sourceView(又名 inView)和 sourceRect(又名 fromRect)。这给出了与之前的 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];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 iOS 8 中执行“actionSheet showFromRect”? 的相关文章

随机推荐