Iphone 弹出视图的定位问题
当我在iPad上从纵向视图切换到横向视图(反之亦然)时,我的popover视图的位置会变得混乱。下面是我如何计算popover视图的帧:Iphone 弹出视图的定位问题,iphone,objective-c,ipad,uipopovercontroller,Iphone,Objective C,Ipad,Uipopovercontroller,当我在iPad上从纵向视图切换到横向视图(反之亦然)时,我的popover视图的位置会变得混乱。下面是我如何计算popover视图的帧: aRect = self.myElement.frame; aRect.origin.x += aRect.size.width; [aPopOver presentPopoverFromRect:aRect inView:self.myElement.superview permittedArrowDirections:UIPopoverArrowDire
aRect = self.myElement.frame;
aRect.origin.x += aRect.size.width;
[aPopOver presentPopoverFromRect:aRect inView:self.myElement.superview permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
请告诉我这里出了什么问题?好的,我注意到你的代码有些奇怪 您将宽度的大小添加到aRect的x位置原点的原因是什么? aRect.origin.x+=aRect.size.width 我假设你希望这是右上角 您可以取消对.m文件中的代码的注释,并将其设置为:
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
Return YES; // for supported orientations
//otherwise return (interfaceOrientation == UIInterfaceOrientationLandscape); if you want only landscape mode.
}
或者,如果您想布局子视图,我会在您的情况下使用DidRotateFromintFaceOrientation:like so:
(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
[self layoutSubviews];
}
以及布局子视图
- (void)layoutSubviews
{
NSLog(@"layoutSubviews called");
...recalc rects etc based on the new self.view.bounds...
}
它是这样工作的
PK来自:(强调矿)
如果用户在移动设备时旋转设备
popover是可见的,popover
控制器隐藏popover,然后
在结尾处再次显示它
轮换。popover控制器
尝试定位popover
适合您,但您可能有
再次呈现或隐藏它
在某些情况下完全是这样。例如
从条形按钮项显示时,
popover控制器的自动控制
调整位置(并可能
要帐户的popover的大小)
用于更改杆的位置
按钮项。但是,如果删除
在启动过程中,显示“栏”按钮项
旋转,或如果呈现
中目标矩形的弹出框
查看,popover控制器不会
尝试重新定位popover。在里面
这些情况下,您必须手动隐藏
popover或从
合适的新职位。你可以
在办公室里做这件事
方法创建的视图控制器
用来展示爆米花
这是一个老问题,但我看不清楚他应该如何处理克里斯·马克尔的建议。这在中有记录。再把爆米花拿出来 假设您将上面的popover代码放在一个名为
showPopover
的方法中。只需在旋转时调用该方法,首先确保其可见:
-(void)showPopover {
aRect = self.myElement.frame;
aRect.origin.x += aRect.size.width;
[self.aPopOver presentPopoverFromRect:aRect inView:self.myElement.superview permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
}
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)oldOrientation
{
if ([self.aPopover isPopoverVisible]) {
[self showPopover];
}
}
在viewcontroller中使用下面提到的UIPopoverPresentationControllerDelegate方法
func popoverPresentationController(_ popoverPresentationController: UIPopoverPresentationController,
willRepositionPopoverTo rect: UnsafeMutablePointer<CGRect>,
in view: AutoreleasingUnsafeMutablePointer<UIView>) {
rect.pointee = self.view.bounds
}
func-popoverPresentationController(\uPopOverpresentationController:UIPopoverPresentationController,
WillPopOverto-rect:UnsafemeutablePointer,
视图中:AutoreleasingUnsafeMutablePointer){
rect.pointee=self.view.bounds
}
<代码>是的,我正在尝试在弹出式视图中间的右侧对齐。通过在原点添加宽度,我可以在启动应用程序时将其放置在正确的位置。当我更改方向时会出现问题。所以你应该尝试didRotateFromInterfaceOrientation。我将更新我的帖子,告诉你如何做到这一点。问一个问题,你的应用程序中有选项卡栏吗?好的。但我应该用“didRotateFromInterfaceOrientation”方法编写什么代码。我的弹出显示逻辑与控制器方法紧密耦合,而且,我的视图由自定义UI小部件组成。在一个这样的UI小部件上,我显示了这个弹出视图。@abhinav在didRotateFromInterfaceOrientation中,您只需编写[self LayoutSubview];这是一个名为which的方法,您应该创建它,它只包含只更新GUI的代码。就这些。在layoutSubviews方法中,它同样只包含更新图形用户界面的代码。所以你会有button.x=here和aRect.origin.x+=where;并重新定位显示器,使其适合横向模式。就这样。用控制器方法填充的显示逻辑是不相关的。您只需要更新GUI.Ok。但我应该用“didRotateFromInterfaceOrientation”方法编写什么代码。我的弹出显示逻辑与控制器方法紧密耦合,而且,我的视图由自定义UI小部件组成。在一个这样的UI小部件上,我显示了这个弹出视图。