⬆️ ⬇️

Popup windows. Work with UIPopoverController

Several new interface elements have been added to the iPhone SDK 3.2, such as the UIPopoverController and the UISplitViewController.

In this post I will show how to use UIPopoverController.



To get started, download the source code from my previous article .

Open Interface Builder and create a new blank document.

Set the File's Owner class to ImageScrubberPopupController, add a UIView and a UIImageView as a child of a UIView.

Save the document and select the “Write class files” command.

Save the class as ImageScrubberPopupController.



Now, list the UIViewController parent class and add a member of the UIImageView * imgView class.

Add a property for this field to access it from the outside with the IBOutlet specifier.

Return to Interface Builder, draw the links between the imgView field and the UIImageView form element and the view field and the UIView element.



Further, we slightly modify ImageScrubberToolbar to work correctly with a pop-up window.

')

UIPopoverController contains 2 methods for showing a popup window:



As follows from the names of the methods, the first shows a pop-up window from the center of the specified rectangle

image



and the second is from the panel button, similar to the pop-up window in the UISplitViewController in portrait orientation.

image



We will use the first method, so we need a rectangle of the selected thumbnail to display a pop-up window coming out of it.

-(CGRect) frameOfSelection

{

return CGRectMake(self.frame.origin.x + left + SMALL_SIZE*position - SIZE_DIF,

self.frame.origin.y - LARGE_SIZE,

LARGE_SIZE,

LARGE_SIZE);

}




* This source code was highlighted with Source Code Highlighter .


Next, add the UIPopoverControllerDelegate to the protocols of the class ImageScrubberViewController.

This protocol contains 2 optional (optional to implement) methods:



To create a pop-up window, all you need to do is call - (id)initWithContentViewController:(UIViewController *)viewController , passing the - (id)initWithContentViewController:(UIViewController *)viewController required for display. If necessary, also set the delegate property.



There is one interesting feature of the UIPopoverController.

If the UIViewController with the contents of the pop-up window is also an instance of the UINavigationController class, the pop-up window will have a wide upper border with all the elements of the navigation bar specified by the UINavigationController.

image



To display a pop-up window, set the popoverContentSize property and call one of the methods:





If the application supports multiple device orientations, you must override the method -(void) didRotateFromInterfaceOrientation: for UIViewController.

If the pop-up window is displayed at the time of the turn, you need to hide it and show it again.

- ( void )didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

{

[imageScrubberToolbar rebuild];



if ([popoverController isPopoverVisible])

{

[popoverController dismissPopoverAnimated:NO];



CGSize size = [self rotatedSize];

size.height -= imageScrubberToolbar.frame.size.height;



popoverController.popoverContentSize = size;



[popoverController presentPopoverFromRect:[imageScrubberToolbar frameOfSelection]

inView:self.view

permittedArrowDirections:UIPopoverArrowDirectionDown

animated:YES];

}

}




* This source code was highlighted with Source Code Highlighter .


When the interface is rotated, the height and width of the UIView DO NOT CHANGE THE PLACES!

Therefore, we add a method that returns the actual window size.

If the default interface orientation is PORTRAIT, use the following code:

-(CGSize)rotatedSize

{

if ((self.interfaceOrientation == UIInterfaceOrientationPortrait) ||

(self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))

{

return self.view.frame.size;

}

else

{

return CGSizeMake(self.view.frame.size.height, self.view.frame.size.width);

}

}



* This source code was highlighted with Source Code Highlighter .


Otherwise, replace UIInterfaceOrientationPortrait and UIInterfaceOrientationPortraitUpsideDown with UIInterfaceOrientationLandscapeRight and UIInterfaceOrientationLandscapeLeft.



That's all. We added a nice popup window to our scrollbar.

image

Source code can be downloaded here .

Source: https://habr.com/ru/post/91094/



All Articles