gpt4 book ai didi

objective-c - IOS8如何移动一个事件的popover

转载 作者:太空狗 更新时间:2023-10-30 03:33:21 28 4
gpt4 key购买 nike

我已经为 iOS7 开发了一个应用程序,现在正尝试为 iOS8 更新它。我遇到的问题如下:

应用程序屏幕方向可以旋转,在某些情况下,一些按钮会大幅移动。我有一些指向这些按钮的弹出窗口,所以如果在屏幕旋转、按钮移动时弹出窗口打开,我也需要弹出窗口。

在 iOS7 中,我通过以下方式做到了这一点:当屏幕旋转时我更新了约束

- (void) updateViewConstraints 
{
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
{
self.Button.constant = (CGFloat)10;
}
else
{
self.Button.constant = (CGFloat)5;
}
[super updateViewConstraints];
}

我也移动了弹出框

- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{

if(TempDisplayPopoverController == examplePopoverController)
{
[examplePopoverController presentPopoverFromRect:[self ExamplePopoverPosition] inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}

我最初加载弹出窗口

- (void) LoadPopover{
examplePopover = [[examplep alloc] initWithNibName:@"exampleP" bundle:nil];
[examplePopover setDelegate:self];
examplePopoverController = [[UIPopoverController alloc] initWithContentViewController: examplePopover];
[examplePopoverController setDelegate:self];

examplePopoverController.popoverContentSize = examplePopover.view.frame.size;

TempDisplayPopoverController = examplePopoverController;


if ([examplePopoverController isPopoverVisible])
{
[examplePopoverController dismissPopoverAnimated:YES];
}
else
{
[examplePopoverController presentPopoverFromRect:[self ExamplePopoverPosition] inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}

[self ExamplePopoverPosition]只是返回按钮位置。

这一切都很好,我很开心,iPad 很开心,一切都很乖。

现在由于 iOS8,我必须更改一些位。

self.interfaceOrientation折旧

[examplePopoverController presentPopoverFromRect:[self ExamplePopoverPosition] inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

didRotateFromInterfaceOrientation抛出错误

"Application tried to represent an active popover presentation: <UIPopoverPresentationController: 0x7bf59280>"

我已设法纠正 self.interfaceOrientation通过

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[self SetUpScreen:toInterfaceOrientation];
}

- (void) SetUpScreen:(UIInterfaceOrientation)toInterfaceOrientation{
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
self.Button.constant = (CGFloat)10;
}
else
{
self.Button.constant = (CGFloat)5;
}
[super updateViewConstraints];
}

但不知道如何解决弹出窗口问题。我试过了

popoverController: willRepositionPopoverToRect: inView:

但似乎无法让它发挥作用。

任何人都可以建议

谢谢

最佳答案

在 iOS 8 中你可以使用 -viewWillTransitionToSize:withTransitionCoordinator:处理屏幕尺寸(和方向)变化:

- (void)viewWillTransitionToSize:(CGSize)size
withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
[_popover dismissPopoverAnimated:NO];
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
// Update your layout for the new size, if necessary.
// Compare size.width and size.height to see if you're in landscape or portrait.
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
[_popover presentPopoverFromRect:[self popoverFrame]
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:NO];
}];
}

当您实现此方法时,像 willAnimateRotationToInterfaceOrientation: 这样的弃用旋转方法在​​ iOS 8 上运行时将不会被调用。

关于objective-c - IOS8如何移动一个事件的popover,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26190909/

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