issue with positioning of pop over view

走远了吗. 提交于 2019-12-23 13:25:15

问题


When I am switching between Portrait to Landscape view (&Vice Versa) in iPad, position of my popover view gets garbled. Here is how I am calculating frame of my popover view:

aRect = self.myElement.frame;
aRect.origin.x += aRect.size.width;

[aPopOver presentPopoverFromRect:aRect inView:self.myElement.superview permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];

Please tell me whats wrong here?


回答1:


ok, i notice something weird about your code.

any reason you are adding the size of the wide to the origin of aRect's x position? aRect.origin.x += aRect.size.width;

im assuming you want this to be the top right corner....

You can uncomment the code in your .m file and make it like so:

// 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.
}

Or what i would do in your situation if you want to layout your subviews is use the didRotateFromIntferfaceOrientation: like so:

(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    [self layoutSubviews];
}

and also layoutSubviews

- (void)layoutSubviews
{
    NSLog(@"layoutSubviews called");

    ...recalc rects etc based on the new self.view.bounds...
}

It works like so.

PK




回答2:


From the UIPopoverController documentation: (emphasis mine)

If the user rotates the device while a popover is visible, the popover controller hides the popover and then shows it again at the end of the rotation. The popover controller attempts to position the popover appropriately for you but you may have to present it again or hide it altogether in some cases. For example, when displayed from a bar button item, the popover controller automatically adjusts the position (and potentially the size) of the popover to account for changes to the position of the bar button item. However, if you remove the bar button item during the rotation, or if you presented the popover from a target rectangle in a view, the popover controller does not attempt to reposition the popover. In those cases, you must manually hide the popover or present it again from an appropriate new position. You can do this in the didRotateFromInterfaceOrientation: method of the view controller that you used to present the popover.




回答3:


This is an old question, but I see it wasn't clear to OP what he should do with Kris Markel's suggestion. This is documented in Technical Q&A QA1694. Just present the popover again.

Let's say you've put your popover code above in a method called showPopover. Just call that method on rotation, first making sure it's visible:

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



回答4:


Use the below mentioned UIPopoverPresentationControllerDelegate method in the viewcontroller.

func popoverPresentationController(_ popoverPresentationController: UIPopoverPresentationController,
                                   willRepositionPopoverTo rect: UnsafeMutablePointer<CGRect>,
                                   in view: AutoreleasingUnsafeMutablePointer<UIView>) {
    rect.pointee = self.view.bounds
}


来源:https://stackoverflow.com/questions/3859842/issue-with-positioning-of-pop-over-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!