Programmatically resizing a nib makes inactive area - ios

I have an iPad app with a split view controller which implements some code to show/hide the tableview on the left.
The right hand side (the detail controller) is a custom UIView (specifically) which is loaded from a nib at runtime - this works fine.
If I hide the master then the detail resizes to fill the screen, as you'd expect, but an area suspiciously similar in width to that of the master tableview is then inactive along the far right of the detail - buttons placed there can not be clicked and you cannot scroll there.
I assume somehow that the resizing is causing this to happen but I cannot work out why...
As requested I use the following code to hide the master:
SplitViewController *splitViewController = (SplitViewController *)[[[[UIApplication sharedApplication] delegate] window] rootViewController];
splitViewController.shouldHideMasterController = !splitViewController.shouldHideMasterController;
[splitViewController willRotateToInterfaceOrientation:self.interfaceOrientation duration:0];
[splitViewController.view setNeedsLayout];
[self updateTextView];

I see this kind of behaviour happen a lot when developing iOS app, the thing to realise is that UIViews do not crop drawing of their subviews but will not let you interact with subview that are outs side their bounds. Check that your content that you can not click on is not outside the frame of any of its superviews.

Related

hidesBarsOnSwipe doesn't work

Have a following structure:
UIViewController (VC) with some UIViews(views) on the top half of screen and UITableView (tableView) on the bottom half of screen. All are on the same hierarchy level within VC's view.
VC is managed by UINavigationController.
Auto-layout is off. Using autoresizing masks.
dataSource and delegate methods are set up and works fine.
What I want to realize:
Hide a navigation bar when scroll up and show in after the scroll down.
What I did:
Since my project supports iOS8+ only, just setting:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.navigationController.hidesBarsOnSwipe = YES;
}
should be fine.
What I have:
Swiping up and down on the views hides and shows navigation bar respectively.
Scrolling tableView makes no sense.
Spent some time on that and found that resizing tableView to make it match to vc's view solves the problem.
Edit:
Is it mentioned somewhere in the docs that table view should be only full screen?
Found interesting thing: even if you didn't set hidesBarsOnTap to YES and try to tap on navigation area (i.e. when it's still hidden after swipe) - navigation bar will show after that. Very annoying if you have any UI elements in that area.

How to hide iOS tabbar and fill the space it leaves

I'm having trouble using the space left behind by my hiding a UITabBar.
I have a UITabBarController with a UINavigationController, whose root view controller just has a UIWebView. I'm using auto-layout constraints to make the webview fill the view which works fine (and fills the available space) when I hide the navigation/status bars.
However, when I hide the tab bar (using setNavigationBarHidden), the tab bar is hidden but the space it vacates isn't used up by the webview, as expected.
Searching similar questions suggest using hidesBottomBarWhenPushed before pushing the view. I don't want to have to push another VC (or have to reload the webview) when rotating the device. Others suggest to resize the frame (like https://stackoverflow.com/a/1554637/1429412 ), but I have not been able to do that successfully.
What is the best way to hide the tab bar in one orientation, but not the other, and have a view fill the space vacated by the tab bar?
Portrait screenshot, showing UIWebview with a green background behind status/nav bar and tab bar as expected: http://i.stack.imgur.com/Hzj9k.png
Landscape screenshot showing hidden status/navbar and tab bar. The webview contents have extended to the top (navbar), but not so to the bottom (tab bar). However, the webview itself (the only element with a green background) has extended down just not its contents (regardless of length): http://i.stack.imgur.com/sdk75.png
If I examine the views at runtime (using FLEX), and click the viewable content area, it shows UIWeBrowserView view with shorter 271 height. Clicking the area where the tabbar space is shows _UIWebViewScrollView with the full 320 height. In code, I can see the UIWebView's .contentSize is only 568x271. I tried various combinations of setting the frame on the webview and its subviews, without success.
Right now, without any frame resizing hacks, the code is simply:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[[self navigationController] setNavigationBarHidden:UIInterfaceOrientationIsLandscape(toInterfaceOrientation) animated:YES];
[[UIApplication sharedApplication] setStatusBarHidden:UIInterfaceOrientationIsLandscape(toInterfaceOrientation) withAnimation:UIStatusBarAnimationSlide];
self.tabBarController.tabBar.hidden = UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}
I'm using xCode 6.1 for an iOS7+ project.
What should I do next? Thanks in advance for any pointers!
That is simply the way Tabbars are suppose to work, The only form of UIView that allows tab bors to be covered are modal views presentviewcontroller .... dismissviewcontroller Apple has a lot of samples just search for it.

resize content after iPhone status bar is shown/hidden

i have an iPhone application which I want to start with hidden status bar, to show a splash screen, and switch the status bar back in when the content to be shown is loaded (it takes some times since it has to be loaded over the internet), using [[UIApplication sharedApplication] setStatusBarHidden:NO animated:YES]. I want my content view to resize and position itself properly after status bar is shown and hidden. I want my application to be able to run in any device orientation (maybe excluding the topdown orientation as specified in apple guidelines).
My problem is I can't get the application to appear as it should. Either it shows a 20pt blank space after autorotation or is hidden below the status bar when it appears.
What should be the proper way to handle this?
I haven't tested it yet, but i guess same issues would arise by the incoming call bar.
EDIT:
I'll try to explain better what the problem is. When the application starts it has status bar hidden, and in the window coordinate system the top corner of the window's visible area is 0,0 in the window's coordinate system. Then I slide in status bar. This doesn't change window size, hence 0,0 point is now hidden behind status bar. Top left of visible area of the window is 0,20 and I have to move my view to those coordinates (and resize it properly to 320 * 460). But if I rotate the device, then back to the original position, top left coordinate of visible window area becomes 0,0 again, and the window is now sized at 320 * 480.
A workaround is to keep a flag telling whether the phone has been rotated before, but isn't there a way to have the window coordinates not changing when I rotate to another position and then back?
Yes, the same issues come up with the call bar. Alternatively, the audio recording bar does the same thing. (Its easier and maybe cheaper to start the voice memo app recording and come back to your app to test how it handles the extra bar.)
Setting the auto-resize masks on the view is what usually adjusts things for you.
If you are building your UI in XIB, you can play with the settings until your views properly change size when the bar changes or when the device or simulator rotates.
Remember that the mask settings on the contained subviews are important too.
Does #walt's answer really works? I've tried to add this flag to every view controller's view property, but without any luck there, am I missing sth?
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
This solution solved my problem, but it looks ugly. I have to resize the view controller view frame manually.
This should fix your issue
Just a View:
[self.view setFrame: [self.view bounds]];
A view with a scroll view inside
[self.view setFrame: [self.view bounds]];
[self.theScroller setFrame: [self.view bounds]];
"theScroller is the name of my scrollview

iPad Modal UIViewControllers position

How do I position this ViewController over the detail view modally? I want it to be right aligned so you can see the navigation portion greyed out.
[self.window addSubview:self.splitViewController.view];
MyModalViewController *modalvc= [MyModalViewController new]; //brevity
modalvc.modalInPopover = YES;
modalvc.modalPresentationStyle = UIModalPresentationStylePageSheet;
modalvc.view.autoresizeMask = UIViewAutoresizeFlexibleRightMargin;
[self.splitViewController presentModalView:modalvc animated:NO];
[self.window makeKeyAndVisible];
I also checked in MyModalViewController if im setting the views mask and I am not, nor is it getting magic values from a Nib.
Adjusting the frame before the present (using modalvc.view.frame) does nothing.
Adjusting the frame after the present seems to yield crazy results, and I only really need it to be half a width over in landscape... portrait is normal behavior.
edit
the picture confused people so I took it out, I dont want the modal view to be the size of the screen, I want to keep it UIModalPresentationStylePageSheet but have its ORIGIN moved right so that it covers the detail view portion in landscape
The modalPresentationStyle is what controls that. You've set it to UIModalPresentationStylePageSheet, which sets the height to the height of the screen, and the width to to the width of the screen in portrait orientation, exactly as in your screenshot.
I think the only way to get full width in landscape is to use UIModalPresentationFullScreen. See the UIViewController reference for more info.
I had a similar problem, and I ended up using a custom view controller that uses a background translucent view and a foreground opaque view that I am able to position anywhere I want by manipulating its frame. It's useful as a lightbox for videos and images.

How come some of my UIViews are shifted after navigation?

In some of my application designs or for just some UIViews, following a navigationController's pushViewController, my new view will be shifted off the window by the height of the status bar. As a result, I will put this code stub in the viewDidLoad method.
CGRect frameAt = [self.view frame];
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
frameAt.origin.y += statusBarFrame.size.height;
[self.view setFrame: frameAt];
It does not make sense to me that this is the intention of XCode and Interface Builder, so I suspect that I am doing something fundamentally wrong with the SDK during my view design. Furthermore, on the rare occasion that I don't have to shift my view, I really don't know what the difference in the two design approaches.
Note also, that most of the time I try to design my views using IB, with some minor customization.
Does anyone else run into this and know what they do to fix without such a code stub?
I've used Apple's NavBar sample code to try and reproduce this problem.
the applicationDidFinishLaunching is originally implemented like this:
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
If I change it to this:
UIViewController *shellController = [[UIViewController alloc] initWithNibName:nil bundle:nil];
[shellController.view addSubview:navigationController.view];
[window addSubview:shellController.view];
[window makeKeyAndVisible];
Then I get the gap appearing.
However if I only do this:
UIView *shell = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[shell addSubview:navigationController.view];
[window addSubview:shell];
[window makeKeyAndVisible];
Then everything looks normal.
So I guess every view controller will offset its view if its the root view controller. But the navigation controller overrides this behaviour to offset its view regardless of whether it's the root view controller. Therefore I would say your gap is appearing because you've got a navigation controller somewhere lower in the hierarchy than it's supposed to be.
The main thing to keep in mind here is that a view controller will set the frame of its view itself. This is because the amount of space available to the view may change during the lifetime of the app, and only the view controller knows how to adjust the view's frame appropriately. Examples of when the amount of space changes include the navigation bar changing height, turning the device from portrait to landscape and the status bar can also increase in height if the user is taking a call. Because of this you should not modify the view's frame yourself.
So, the first thing you need to is is remove all code related to modifiying the view's frame.
Now you need to design your views with the mindset that the frame size could change at any moment. This means setting the autoresizing property of each subview properly. If you do this, then it won't matter if you turn on the simulated navigation and status bars or not; they're just there to help you see what the final result will look like in most cases.
You can set the autoresizing property of each subview in Interface Builder in the Size Inspector (the one with the ruler icon). In the animation, the white box represents the root view of the view controller, the red box represents the currently selected subview. You'll notice that the subview is anchored to the top-left corner of the root view by default. This is fine if the size of the view never changes, but we know that not to be true. If you have subviews that you want to appear at the bottom no-matter-what, then you need to play with the diagram to the left. The way it works is if one of the four lines around the edge is selected, then the distance between that edge of the root view and the edge of the subview is fixed. So if you want a subview to appear at the bottom, you need to make sure the bottom-most line is selected and not the top. The two lines in the middle affect whether the size of the subview changes when the root view change size. So, for example, if you had a table view that you wanted to occupy the entire height of the screen, you would make sure the inner vertical line was selected. This is called the struts and springs model.
If you are adding subviews programatically you need to set the autoresizingMask property on each subview. Here's an explanation.
Hope that helps!
I've run into similar issues. Check out my two previous questions:
IPhone - After dismissing Modal View Controller - gap is left at top of page
IPhone - UIView addSubview Gap at top
link text
A similar bug is discussed here.
Also is animation set to NO? Try setting it to YES as this solved a similar problem I was facing.
Moshy's answer was very helpful as I finally realized the meaning of the dotted/solid lines in IB for controlling the resize properties of UIView elements.
However, adjusting those properties did not address a similar problem I faced with one of my views. This view had a status and top bar defined in IB. It was a slightly heavy one,
containing a UIWebView that would load a HTML string within viewWillAppear and a few other interface elements.
While loading the view, if the user suddenly changed the orientation of the device from portrait to landscape, all contents of the view would shift downwards by the height of the status bar. The resulting gap between the view controls and its top would remain even after switching back to portrait orientation.
What finally solved my issues, and my remaining hair, was adding the line:
self.view.frame = [[UIScreen mainScreen] bounds];
within
-(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
Ever since my view contents are held in place despite abrupt device orientation changes.

Resources