How to do “actionSheet showFromRect” in iOS 8?

前端 未结 4 630
旧巷少年郎
旧巷少年郎 2021-01-13 16:03

In iOS 7, I show actionSheet by \"showFromRect\":

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

But in iOS 8, this doesn\'t wor

4条回答
  •  孤独总比滥情好
    2021-01-13 16:25

    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];
    

提交回复
热议问题