我试图从UITableViewCell
上的按钮在iPad上显示UIActionSheet
。它在iOS 7上运行良好,但在iOS 8上运行不正常。UIActionSheet
包含一些视觉工件,我在控制台中得到这个警告:
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x79793de0 H:[UIView:0x797a7630(304)]>",
"<NSLayoutConstraint:0x7a16ae00 UIView:0x7a16ab60.width == _UIAlertControllerView:0x797a9230.width>",
"<NSAutoresizingMaskLayoutConstraint:0x7a66f2a0 h=--& v=--& H:[UIView:0x7a16ab60(298)]>",
"<NSLayoutConstraint:0x797a49e0 _UIAlertControllerView:0x797a9230.width >= UIView:0x797a7630.width>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x79793de0 H:[UIView:0x797a7630(304)]>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
我代码:- (void)onFavoriteBtn:(id)sender {
CGPoint btnPosition = [sender convertPoint:CGPointZero toView:_tableView];
NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:btnPosition];
if (indexPath) {
UIButton *favoriteBtn = (UIButton *)sender;
CGRect favoriteBtnRect = favoriteBtn.frame;
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Remove from Favorites" otherButtonTitles:nil];
[sheet showFromRect:favoriteBtnRect inView:favoriteBtn.superview animated:true];
}
}
UIActionSheet
已弃用,因此您可以使用iOS 8.0或更高版本的UIAlertViewController
。
- (IBAction)onFavoriteBtn:(id)sender {
CGPoint btnPosition = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:btnPosition];
if (indexPath) {
[self showActionSheet:sender];
}
}
- (void)showActionSheet:(UIView *)sender
{
NSString *alertTitle = NSLocalizedString(@"ActionTitle", @"Archive or Delete Data");
NSString *alertMessage = NSLocalizedString(@"ActionMessage", @"Deleted data cannot be undone");
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:alertTitle
message:alertMessage
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel action")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
NSLog(@"Cancel action");
}];
UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Delete", @"Delete action")
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action)
{
NSLog(@"Delete action");
}];
UIAlertAction *archiveAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Archive", @"Archive action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(@"Archive action");
}];
[alertController addAction:cancelAction];
[alertController addAction:deleteAction];
[alertController addAction:archiveAction];
UIPopoverPresentationController *popover = alertController.popoverPresentationController;
if (popover)
{
popover.sourceView = sender;
popover.sourceRect = sender.bounds;
popover.permittedArrowDirections = UIPopoverArrowDirectionAny;
}
[self presentViewController:alertController animated:YES completion:nil];
}
看起来你最喜欢的按钮父视图太小了,无法嵌入动作表。尝试使用窗口来显示动作表,只是不要忘记转换收藏按钮的坐标:
- (void)onFavoriteBtn:(id)sender {
CGPoint btnPosition = [sender convertPoint:CGPointZero toView:_tableView];
NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:btnPosition];
if (indexPath) {
UIButton *favoriteBtn = (UIButton *)sender;
CGRect frame = [self.window convertRect:favoriteBtn.bounds fromView:favoriteBtn];
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Remove from Favorites" otherButtonTitles:nil];
[sheet showFromRect:frame inView:self.window animated:true];
}
}