升级到iOS 11

时间:2017-11-20 14:47:58

标签: objective-c uibarbuttonitem ios11 uipopovercontroller uipopover

自升级到iOS 11以来,我开始遇到错位的弹出窗口问题,弹出窗口通常将presentPopoverFromRect:sender.frame的框架设置为导航栏按钮的坐标,触发了操作。

问题:(UIButton *)sender.frame的x和y坐标等于{0,0} ,并且弹出框显示在屏幕的左上角。

以下是generateToolbar()方法,可生成用户导航按钮。

-(void) generateToolbar {

    // Initialize the `ButtonFactory`

    ButtonFactory *buttonFactory = [[ButtonFactory alloc] init];

    // Generate some UIButton(s)

    UIButton *userUIBtn = [buttonFactory createButtonWithButtonType:ButtonTypeUser iconVisibility:YES textVisibility:NO capitalization:NO iconSize:20 textSize:10];
    userUIBtn.frame = CGRectMake(0,0,55,20);
    [userUIBtn addTarget:self action:@selector(userButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

    // Generate an `UIBarButtonItem` with the `UIButton` as basis

    UIBarButtonItem *userBtn = [[UIBarButtonItem alloc] initWithCustomView:userUIBtn];

    // Add the `UIBarButtonItem` into the right side of the `navigationItem`

    self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects: userBtn, nil];

    DDLogVerbose(@"A: generateToolbar UIButton FRAME: %@",NSStringFromCGRect(userUIBtn.frame));

}

以下是userButtonPressed()方法,按用户按钮触发该方法,并在该按钮周围生成弹出窗口。

- (IBAction) userButtonPressed:(UIButton *)sender {

    // Initialize the `ButtonFactory`

    ButtonFactory *buttonFactory = [[ButtonFactory alloc] init];

    // Generate some UIButtons        

    UIButton *feedbackUIBtn = [buttonFactory createButtonWithButtonType:ButtonTypeFeedback iconVisibility:YES textVisibility:YES capitalization:NO iconSize:20.0 textSize:20.0];
    [feedbackUIBtn addTarget:self action:@selector(feedbackBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    feedbackUIBtn.frame = CGRectMake(0,50,300,50);

    // Create a `UIViewController` and add the `UIButtons` as subviews

    UIViewController *viewController = [[UIViewController alloc] init];
    [viewController.view addSubview:feedbackUIBtn];
    [viewController.view addSubview:crashUIBtn];

    // Create the `UIPopoverController`

    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
    self.mainPopoverController = [[UIPopoverController alloc] initWithContentViewController:navigationController];
    self.mainPopoverController.popoverContentSize = CGSizeMake(300, 100); 

    DDLogVerbose(@"B: userButtonPressed SENDER FRAME: %@",NSStringFromCGRect(sender.frame));

    // Present the `UIPopoverController`

    [self.mainPopoverController presentPopoverFromRect:sender.frame inView:self.view permittedArrowDirections: UIPopoverArrowDirectionLeft | UIPopoverArrowDirectionUp animated:YES];
}

日志输出 - 在两个地方x,y坐标都等于{0, 0},这是错误的,它们应该是非零且等于UIBarButtonItem的实际协调值:

A: generateToolbar UIButton FRAME: {{0, 0}, {55, 20}}
B: userButtonPressed SENDER FRAME: {{0, 0}, {55, 32}}

1 个答案:

答案 0 :(得分:1)

“到导航栏按钮的坐标”这个概念毫无意义,因为UIBarButtonItem不是UIView且没有frame。将弹出箭头指向UIBarButtonItem的方法是设置弹出窗口控制器的barButtonItem。 (popover 演示文稿控制器:你不应该在这个时代使用UIPopoverController。)