2015-01-21 38 views
0

自iOS8以來,Apple似乎改變了UIPopoverController的態度。iOS8:不顯示UIPopoverController但iOS7確定

請看下面的代碼。我想在addSubview之後顯示UIPopoverController。 iOS6和iOS7沒有問題,但不會在iOS8上顯示。

你能提出任何可能的原因嗎?任何幫助將不勝感激。

- (IBAction)tapButton:(id)sender { 

    SampleView *sampleView = [[SampleView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 
                      [UIScreen mainScreen].bounds.size.width, 
                      [UIScreen mainScreen].bounds.size.height)]; 
    sampleView.backgroundColor = [UIColor blueColor]; 
    sampleView.alpha = 0.5; 

    sampleView.label = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2, 
                   [UIScreen mainScreen].bounds.size.height/2, 
                   200.0f, 20.0f)]; 
    sampleView.label.text = @"SAMPLE TEXT"; 
    sampleView.label.textColor = [UIColor redColor]; 
    [sampleView addSubview:sampleView.label]; 

    [self.view.window addSubview:sampleView]; 

    if (!self.popover) { 
     UIViewController *vc = [[UIViewController alloc] init]; 
     vc.view.backgroundColor = [UIColor redColor]; 
     vc.preferredContentSize = CGSizeMake(200, 300); 
     self.popover = [[UIPopoverController alloc] initWithContentViewController:vc]; 
    } 

    // On iOS7 is OK but not show on iOS8 
    [self.popover presentPopoverFromRect:CGRectMake(200, 100, 0, 0) 
            inView:sampleView.label 
       permittedArrowDirections:UIPopoverArrowDirectionUp 
           animated:YES]; 
} 

enter image description here

enter image description here

UPDATE1:

我接着這個問題的答案。

in iOS8: UIPopoverController presentPopoverFromRect not work for keyWindow any more

這是更新的代碼。

[self.popover presentPopoverFromRect:CGRectMake(200, 100, 0, 0) 
            inView:self.view 
       permittedArrowDirections:UIPopoverArrowDirectionUp 
           animated:YES]; 

enter image description here

雖然我查遠,keyWindos和self.view.window似乎是相同的。

NSLog(@"%@", [UIApplication sharedApplication].keyWindow); 
NSLog(@"%@", self.view.window); 

但是,這不是我的情況的完整解決方案。我想在添加的子視圖上控制視圖。

UPDATE2:

我想我需要使用UIPopoverPresentationController上iOS8上。

- (IBAction)tapButton:(id)sender { 

    // View 
    SampleView *sampleView = [[SampleView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 
                      [UIScreen mainScreen].bounds.size.width, 
                      [UIScreen mainScreen].bounds.size.height)]; 
    sampleView.backgroundColor = [UIColor blueColor]; 
    sampleView.alpha = 0.5; 

    // Label 
    sampleView.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 20.0f)]; 
    sampleView.label.text = @"SAMPLE TEXT"; 
    sampleView.label.textColor = [UIColor redColor]; 
    [sampleView addSubview:sampleView.label]; 

    // View Controller 
    UIViewController *viewController = [[UIViewController alloc] init]; 
    viewController.modalPresentationStyle = UIModalPresentationPopover; 
    viewController.view = sampleView; 

    // UIPopoverPresentationController 
    UIPopoverPresentationController *presentationController = viewController.popoverPresentationController; 
    [presentationController setDelegate:self]; 
    presentationController.permittedArrowDirections = 0; 
    presentationController.sourceView = [[UIApplication sharedApplication] keyWindow]; 
    presentationController.sourceRect = [[UIApplication sharedApplication] keyWindow].bounds; 

    [viewController setPreferredContentSize:CGSizeMake(320, 480)]; 
    [self presentViewController:viewController animated:YES completion:nil]; 

    if (!self.popover) { 
     UIViewController *vc = [[UIViewController alloc] init]; 
     vc.view.backgroundColor = [UIColor redColor]; 
     vc.preferredContentSize = CGSizeMake(200, 300); 
     vc.modalPresentationStyle = UIModalPresentationPopover; 
     self.popover = [[UIPopoverController alloc] initWithContentViewController:vc]; 
     self.popover.delegate = self; 
    } 

    [self.popover presentPopoverFromRect:CGRectMake(300, 300, 0, 0) 
            inView:viewController.view 
       permittedArrowDirections:UIPopoverArrowDirectionUp 
           animated:YES]; 
} 

enter image description here

UPDATE3我用dispatch_async,但沒有奏效。有人說dispatch_async在這種情況下是有效的。 dispatch_async是非常簡單的解決方案。我更喜歡使用它。如果你能通過使用dispatch_async來解決我的問題,我將不勝感激你可以附上代碼。

- (IBAction)tapButton:(id)sender { 

    SampleView *sampleView = [[SampleView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 
                      [UIScreen mainScreen].bounds.size.width, 
                      [UIScreen mainScreen].bounds.size.height)]; 
    sampleView.backgroundColor = [UIColor blueColor]; 
    sampleView.alpha = 0.5; 

    sampleView.label = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2, 
                   [UIScreen mainScreen].bounds.size.height/2, 
                   200.0f, 20.0f)]; 
    sampleView.label.text = @"SAMPLE TEXT"; 
    sampleView.label.textColor = [UIColor redColor]; 
    [sampleView addSubview:sampleView.label]; 

    [self.view.window addSubview:sampleView]; 

    if (!self.popover) { 
     UIViewController *vc = [[UIViewController alloc] init]; 
     vc.view.backgroundColor = [UIColor redColor]; 
     vc.preferredContentSize = CGSizeMake(200, 300); 
     self.popover = [[UIPopoverController alloc] initWithContentViewController:vc]; 
    } 

    // I tried this but not worked. 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     [self.popover presentPopoverFromRect:CGRectMake(200, 100, 0, 0) 
             inView:sampleView.label 
        permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES]; 
    }); 
} 

回答

3

嘗試做了針對iOS 8以下

 
    dispatch_async(dispatch_get_main_queue(), ^{ 
    [self.popover presentPopoverFromRect:CGRectMake(200, 100, 0, 0) inView:sampleView.label permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES]; 
}); 

編輯

這是我的代碼,它的作品有或沒有dispatch_async。

 
self.popover = [[UIPopoverController alloc] initWithContentViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"second"]]; 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     [self.popover presentPopoverFromRect:self.button.frame inView:self.button permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES]; 
    }); 
+0

謝謝。我試過了,但沒有奏效。請參閱我的問題Update3。有人說dispatch_async是有效的,但在我的情況下不起作用。 – zono 2015-01-21 09:36:12

+0

dispatch_async是否正常工作? – zono 2015-01-21 09:50:09

相關問題