gpt4 book ai didi

iOS8:不显示 UIPopoverController 但 iOS7 正常

转载 作者:行者123 更新时间:2023-11-28 19:53:13 25 4
gpt4 key购买 nike

Apple似乎从iOS8开始改变了对UIPopoverController的态度。

请看下面的代码。我想在 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

更新 1:

我跟着这个问题的答案。

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

但是,这不是我的案例的完整解决方案。我想控制添加的 subview 上的 View 。

更新2:

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

- (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];
});
}

最佳答案

尝试针对 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];    });

关于iOS8:不显示 UIPopoverController 但 iOS7 正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28061908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com