How do I resize views when using UITabBarController as popover - resize

I have a UITabBarController that is created as a popover view on an iPad screen. There are three tabs associated with it. I'm trying to make one popover (the first tab) smaller than the other two. Once the popover is created and displayed how can I change the size of the popover window when the first tab is selected vs the other two views?
This is the code that creates the popover.
let storyboard = UIStoryboard(name: "Flight", bundle: nil)
let tbc = storyboard.instantiateViewController(withIdentifier: "flight") as! Flight_TBC
tbc.preferredContentSize = CGSize(width: 750, height: 900)
present(tbc, animated: true, completion: nil)
Within the first view controller I tried this but it doesn't work:
var frame = self.view.frame
frame.size.height = 750
self.view.frame = frame
Thanks for any help!

Related

UIPopoverPresentationController has wrong anchor position on iPhone 6 plus and 7 plus when it's in landscape mode [duplicate]

In my iOS 8 app, this popover segue appears correctly on all devices in all orientations except for iPhone 6 Plus landscape:
This is how it looks on iPhone 6 Plus landscape (it is stretching almost from top to bottom):
And when it displays like this, clicking outside of the view doesn't dismiss it (although Cancel does work). Rotating back to portrait gets it back to normal.
All of the constraints in this UIViewController are installed on all size classes.
When debugging values in viewDidAppear: I see the following:
po self.view: frame = (0 0; 250 394)
po self.preferredContentSize (width = 250, height = 160)
What is causing the view's height to jump to 394?
I'm actually having the same issue with another popover segue in iPhone 6 Plus landscape as well. (And in case there was curiosity, I'm using a VC instead of 'UIAlertController' here because of the validation requirements of the UITextField displayed don't work well with UIAlertController.)
Edit to include my popover code:
This code is found in prepareForSegue:
FavoriteNameViewController *nameVC = segue.destinationViewController;
UIPopoverPresentationController *popPC = nameVC.popoverPresentationController;
popPC.delegate = self;
nameVC.delegate = self;
nameVC.view.center = self.originalContentView.center;
And then the delegate method:
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
return UIModalPresentationNone;
}
And here is the segue definition in Xcode:
What you're seeing is not a popover. It's a normal presented view. By default, a popover appears as a popover on iPad, but as a presented view on iPhone — including the iPhone 6 plus. On other iPhones, this presented view is fullscreen - it covers everything. But the iPhone 6 is so wide that they don't do that, so it appears in the middle of the screen at a standard width (the width of a smaller iPhone).
Thus, the preferred content size has no effect. This isn't a popover. Presented view controller views are given a standard size, and this one is no exception.
However, you can have the popover appear as a popover on iPhone. To do so:
Set a delegate for the popover view controller's popoverPresentationController before presenting it.
In the delegate, implement adaptivePresentationStyleForPresentationController: and return .None.
However, this is apparently not working on iPhone 6 Plus in landscape mode; the popover is not "adapting". I would describe this as a bug!
EDIT In iOS 9, the problem is solved! Implement the new delegate method adaptivePresentationStyleForPresentationController:traitCollection: to return .None and you'll get a popover under all circumstances, including the iPhone 6 Plus in landscape. Here's a complete working example where the popover is created and summoned in code in response to a button tap:
#IBAction func doButton(sender: AnyObject) {
let vc = MyViewController()
vc.preferredContentSize = CGSizeMake(400,500)
vc.modalPresentationStyle = .Popover
if let pres = vc.presentationController {
pres.delegate = self
}
self.presentViewController(vc, animated: true, completion: nil)
if let pop = vc.popoverPresentationController {
pop.sourceView = (sender as! UIView)
pop.sourceRect = (sender as! UIView).bounds
}
}
func adaptivePresentationStyleForPresentationController(
controller: UIPresentationController,
traitCollection: UITraitCollection)
-> UIModalPresentationStyle {
return .None
}

iOS8: How do I make statusBar opaque after navigationBar is hidden using hidesBarsOnSwipe?

I am building iOS8 app. On my tableview controller, I am using self.navigationController.hidesBarsOnSwipe = YES, to hide the navigationBar on swipe up gesture. It is working nicely, but my statusBar becomes transparent and shows the table content underneath.
On storyboard, Status Bar are Top Bar are set to "Inferred"
I want to:
1. Keep my status bar opaque
2. Maintain the same color as the navigationBar
3. Table content scrolls underneath the statusBar
Thank you.
Here is a Swift solution:
First, change UITableViewController to UIViewController and add a tableView field.
Then, implement your viewDidLoad method as follows:
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.frame = view.frame
view.addSubview(tableView)
let topBar = UIView(frame: UIApplication.sharedApplication().statusBarFrame)
topBar.backgroundColor = myDesiredColor
view.addSubview(topBar)
}
You can add a constraint to the top layout, by this scrolling content will not appear below the status bar.
Make a custom View.
UIView * statusBarView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 20)];
statusBarView.backgroundColor=[UIColor whiteColor];
[self.view addSubview:statusBarView];

Prevent hiding keyboard on presentViewController (showing popup)

There is a view with UITextView on it. UITextView is the only one possible firstResponder for keyboard. Let's call it "BackView".
There is another view which is PopUp (full screen view with transparent background which fades underlying view). Let's call it "PopUp".
PopUp works fine except when the keyboard is on screen. Presenting PopUp forces keyboard to be hidden and, when the PopUp is dismissed, the keyboard is shown again. Since the PopUp is not covering whole BackView it does not look good.
Is there any way to keep keyboard on BackView while PopUp is shown? (there is no need to use keyboard in popup).
PopUp contains:
full screen view with background set to black colour with alpha (to fade underlaying view)
several images, labels and Close button.
PopUp is shown with:
[self setModalPresentationStyle:UIModalPresentationCurrentContext];
[self presentViewController:vc animated:YES completion:nil];
Found solution:
Add PopUp view as a subview for current window
mb.server answered his own question (one that I had as well). But I just wanted to spell out the code explicitly for the benefit of others:
//instantiate popUpVC, then
popUpVC.view.frame = [UIScreen mainScreen].bounds; //assuming you wish the popover to occupy the entire screen; adjust as needed
UIWindow* currentWindow = [[[UIApplication sharedApplication] windows] lastObject];
[currentWindow addSubview:popUpVC.view];
Show:
if let popupVC = storyboard.instantiateViewControllerWithIdentifier("PopupVC") as? PopupVC
{
var frame = UIScreen.mainScreen().bounds
frame.origin.y = frame.size.height
overlayWindow = UIWindow(frame: frame)
popupVC.overlayWindow = overlayWindow
overlayWindow.windowLevel = UIWindowLevelAlert
overlayWindow.rootViewController = popupVC
overlayWindow.makeKeyAndVisible()
UIView.animateWithDuration(0.3, animations:
{
self.overlayWindow.frame.origin.y = 0
})
}
Hide in PopupVC
if let window = overlayWindow
{
UIView.animateWithDuration(0.3, animations: {
window.frame.origin.y = window.frame.height
}, completion: { (finished) -> Void in
self.overlayWindow = nil
})
}

How to resize UIPageViewController in landscape

I am having an issue with resizing UIPageViewController in landscape mode.
Here is my scenario:
I have a UIViewController which has a scrollview in it.
I added a UIPageViewController to the scrollview programmatically and it is displaying all my view controllers in each page correctly.
When i change the device orientation to landscape i am correctly changing the content size of the scrollview but the PageViewController is displaying only up to half of its contents. Can you please help me with this one.
Thanks,
Anand.
It looks like you are either not getting the correct orientation of your device or trying to set it in the viewDidLoad function. The following code should correctly set your page view controller's frame to current orientation.
- (void)viewWillLayoutSubviews {
CGRect screenFrame = [[UIScreen mainScreen] applicationFrame];
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
screenFrame = CGRectMake(0, 0, screenFrame.size.height, screenFrame.size.width);
}
//Please don't forget to replace _myPageViewController with your variable
_myPageViewController.frame = screenFrame;
}
Please note that code is getting called from viewWillLayoutSubviews
function.

how to change the height of two views (One view shrink and the other expand) using objective c?

i have two views(A&B) in the same view controller(popOver window) and want to toggle between them, view A is larger than view B, when the popOver window first loads i want view A only to appear and when i press certain button inside it , it should shrink down to let view B appear above it, i tried to set the height of view B to fit into the all window when the window first load(Inside viewDidLoad)then reduce it's height and increase the height of the other view when a button is clicked, i try that code but it didn't change the height of the view !!
-(void)viewDidLoad
{
[super viewDidLoad];
UIView *contactsView=[self.view viewWithTag:2];// is this a correct initialization of a uiView from a view in a storyBoard ?
CGRect frame=contactsView.frame;
frame.size.height+=100;
contactsView.frame=frame;
}
any idea about that will be helpful,thanks.
As a general answer (I don't have time for more detail), I would use 3 views overall:
1) The view owned by your View Controller.
2) View A
3) View B
View A and B can be created in viewDidLoad of your view controller. Then set the frame of each of these for the position and size. Add them both to the view:
// in your .h file create these variable:
UIView* m_vA;
UIView* m_vB;
- (void)viewDidLoad
{
m_vA = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 50.0, 50.0)] autorelease];
m_vB = [[[UIView alloc] initWithFrame:<your rect for View B>] autorelease];
// The views will automatically be retained.
[self.view addSubview:vA];
[self.view addSubview:vB];
m_vB.hidden = YES;
}
// In your button callback, toggle the visibility of views
- (IBAction)buttonPressed:(id)sender
{
// Toggle the visibility of views
m_vA.hidden = !m_vA.hidden;
m_vB.hidden = !m_vB.hidden;
}
// Dont forget to clean up the memory of these views in dealloc and viewDidUnload.