Put a view on top landscape support - ios

I am using the following code to create a view and put it on top:
UIWindow* mainWindow = [[UIApplication sharedApplication] keyWindow];
CGRect viewRect = mainWindow.frame;
topView = [[UIView alloc] initWithFrame:viewRect];
[topView setBackgroundColor:[UIColor colorWithWhite:0.2 alpha:0.4]];
[mainWindow addSubview:topView];
It works perfectly but my problem is if I write anything one the view(like using a Label) and my device is in a landscape position, the text is in vertical position. I've attached a picture to make it more clear. Is there any way to fix it?
EDIT: if I use UIApplication.sharedApplication.keyWindow.rootViewController instead of mainWindow, I will get this:

See this answer:
View on top of everything: UIWindow subview VS UIViewController subview
Basically, only the first subview of the main window gets rotation events, so you have to do it some other way.

There's no way to fix it without adding the given view to a View Controller (UIWindow was never meant to handle rotation, and has no logic to do so).
The wonky view rotation results you're experiencing are actually the result of UIView's default autoresizingMask's
topView = [[UIView alloc] initWithFrame:viewRect];
[topView setBackgroundColor:[UIColor colorWithWhite:0.2 alpha:0.4]];
[topViewsetAutoresizingMask:(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)];

Related

Weird autolayout centering issue

This has been dogging me a bit.
Basically, what it comes down to is that when I manually define autolayout constraints using layout anchors, and use the view controller in a popover, the center is in the wrong place. It's seeming to center around the boundary from the left of the popover pop-out arrow to the left and the right margin, which is not the center of the area of the popover.
This seems to work fine with other UIViewController presentation methods, like UIModalPresentationFormSheet.
Any ideas how I can fix this in a layout-friendly way without special-casing the margins?
PS. The exact same thing happens if you peg the layout to the center of the parent view, it's slightly off.
(note: Edited because I posted the source badly)
-(void)loadView {
// create root view.
UIView *view = [[UIView alloc] init];
[view setBackgroundColor:[UIColor greenColor]];
[view setTranslatesAutoresizingMaskIntoConstraints:NO];
self.view = view;
UIView *parentView = [[UIView alloc] init];
[view addSubview:parentView];
[parentView setTranslatesAutoresizingMaskIntoConstraints:NO];
[parentView setBackgroundColor:[UIColor redColor]];
[parentView.leadingAnchor constraintEqualToAnchor:view.leadingAnchor constant:20.0].active = YES;
[parentView.trailingAnchor constraintEqualToAnchor:view.trailingAnchor constant:-20.0].active = YES;
[parentView.topAnchor constraintEqualToAnchor:view.topAnchor constant:10.0].active = YES;
[parentView.bottomAnchor constraintEqualToAnchor:view.bottomAnchor constant:-10.0].active = YES;
}
The edges of the view are not where you think they are; the leading edge is where the point of the arrow is, not where the side of the visible rectangle is.
However, the margins are where you expect them. So pin to the layoutMarginsGuide instead, and all will be well. Indeed, this is one of the things margins are intended to cover.
In Swift:
parentView.leadingAnchor.constraint(
equalTo: self.view.layoutMarginsGuide.leadingAnchor, constant:0).isActive = true
parentView.trailingAnchor.constraint(
equalTo: self.view.layoutMarginsGuide.trailingAnchor, constant:0).isActive = true
(And change those constant numbers as you see fit.)

iOS: How to keep showing a subView after pushing to a viewController

I have viewController No.1, which has a subView showing. And after I push to another viewController, No.2, this subView does not shown on the screen anymore, which makes sense, since I have pushed to another VC.
However, I hope to keep this subView on the screen even though I am in a pushed viewController now. So I wonder how I can achieve this?
This will work:
//Create a view in the center of the screen
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width/2)-50, ([UIScreen mainScreen].bounds.size.height/2)-50, 100, 100)];
view.backgroundColor = [UIColor redColor];
//Create a window class
UIWindow* window = [[UIApplication sharedApplication].windows objectAtIndex:0];
//Pick a level
window.windowLevel = UIWindowLevelStatusBar;
//Add the view
[window addSubview:view];

Adding MKMapView Causes Status Bar Issue in iOS 7

I'm solving the status bar issue in iOS 7 using
if(st.version == 7)
{
CGRect screen = [[UIScreen mainScreen] bounds];
CGRect frame = self.navigationController.view.frame;
frame.origin.y = 20;
frame.size.height = screen.size.height - 20;
self.navigationController.view.frame = frame;
}
Since I'm using navigation controller and pushing from one to another using [self.navigationController pushViewController:newone animated:YES];. It works fine in all view controllers. But, if the viewcontroller has mkmapview in xib, status bar issue of ios 7 occurs.
If I delete the mapview form xib and push to that view controller means, it will be like,
If I add the mapview even by code below,
mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 100, 320, 100)];
[self.view addSubview:self.mapView];
It looks like,
How to solve this?
if(st.version == 7){
mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 120, 320, 100)];
}else{
mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 100, 320, 100)];
}
If you add mapView in viewWillAppear replace it in viewDidAppear.
Maybe you have this issue because you're doing manipulations with view's frames before your view is completely set up
I would highly suggest against doing it like that.
If you're using interface builder, then add constraints based on how you want your application to look and the frame will auto adjust itself.
If you're not using interface builder, then still use constraints, but get a good tutorial about making constraints programatically (as I don't know how to do it myself).
Edit: The reason I HIGHLY suggest not doing it with hardcoded numbers is that it'll be a pain to do iOS 6/7 Landscape/Portrait 3.5/4 inch screens. That's 8 cases.
i think you have some adjust your navigation Y position set -20px. that way it goes overlay. use this code your ViewController
CGRect screen = [[UIScreen mainScreen] bounds];
CGRect frame = self.navigationController.view.frame;
frame.origin.y =0;
frame.size.height = screen.size.height;
self.navigationController.view.frame = frame;
or may it you have use wantFullScreenLayout some where in your project
setWantsFullScreenLayout = YES:
statusbar section is located to the (0,0) point to catch.
Statusbar and as large as the size of the current view to increase the value of mainScreen change the size of the bounds.
Statusbar change the style of the translucent style.
this below link you get some clear idea about your issue
How do I get the navigation bar in a UINavigationController to update its position when the status bar is hidden?
Override the -edgesForExtendedLayout method in your view controller
-(UIRectEdge)edgesForExtendedLayout {
return UIRectEdgeNone;
}
If you want to hide status bar from a particular view add this method in that particular view.m file
- (BOOL)prefersStatusBarHidden
{
return YES;
}
What about setting self.automaticallyAdjustsScrollViewInsets = NO; in viewDidLoad of your view controller or in IB?
Try to set MapView(ScrollView) automaticallyAdjustsScrollViewInsets = NO;
Try to set edgesForExtendedLayout to UIRectEdgeNone;
Try to use UIViewController.topLayoutGuide, see the Q&A from apple about this issue:Preventing the Status Bar from Covering Your Views.
Try to use the bar position delegation, see UIBarPositioningDelegate Protocol Reference
According to your description and screenshots, you are trying to move the whole UINavigationController.view.frame 20 pt, and the MapView(ScrollView) did something to prevent it happened (or re-set), put some breakpoint and log to track the frame of UINavigationController.view.frame changed.
Could you please provide a sample project? I'm so curious about what really happened.
try this
- (void)viewWillAppear:(BOOL)animated
{
self.navigationController.navigationBar.translucent = NO;
}
- (void)viewDidAppear:(BOOL)animated
{
self.navigationController.navigationBar.translucent = YES;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
[application setStatusBarHidden:YES];
return YES;}
Please add this line to your code.This will hide the status bar from your app.
If you have a xib. Did you try to enabled Top Bar in simulated Metrics ?

Replacing self.view with new UIView shows black view

I want to change the existing view in a UIViewController to a new view. The new view contains the old view and a little banner view.
Doing this fairly simple change leaves me with a black view.
My code looks like this
UIView *existingView = self.view;
UIView *newView = [[UIView alloc] initWithFrame:existingView.frame];
UIView *bannerView = [[UIView alloc] initWithFrame:CGRectMake(0, (self.view.frame.size.height - 50), 320, 50)];
CGRect existingViewFrame = existingView.frame;
existingViewFrame.size.height -= 50;
existingView.frame = existingViewFrame;
[newView addSubview:existingView];
[newView addSubview:bannerView];
self.view = newView;
However when switch Tabs and come back to the view which changed the view is shown just like I want. I guess I need to set a flag or something to tell the controller to redraw it's (new) view.
Edit
I wrote an simple example for this problem. You can find it on GitHub: https://github.com/Oemera/ChangeView
You did not say where you do this. It may be that you need to save the original view's super view, then add the new view to that views subViews array. I'm betting that is the problem.

How to cover also the UIStatusBar with UIView when i cover all the screen with the UIView? (iPhone)

How to cover also the UIStatusBar with UIView when i cover all the screen with the UIView?
I was battling how to do this for a long time too! Finally figured it out :) The key is to set the windowLevel of your new window to really high so it lives on top of all the other windows/views/statusbar etc:
UIWindow *keyWin = [UIApplication sharedApplication].keyWindow;
UIWindow *hudWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0.0f, 0.0f, keyWin.frame.size.width, keyWin.frame.size.height)];
hudWindow.backgroundColor = [UIColor blackColor];
hudWindow.alpha = 0.60;
[hudWindow setWindowLevel:10000.0f];
[hudWindow setHidden:NO];
Enjoy!
The best thing you can do is hide the status bar with:
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO]
and show it again when you need it.
You can basically removeFromSuperview any time you want to remove the view.
If you're trying to create a UIAlertView-like effect, I don't think you can do this. You might file a feature enhancement request with Apple at http://bugreporter.apple.com.
Nice, but I made these two changes. Adding 1.0 to UIWindowLevel still hides the status bar, and I have no idea why.
self.windowLevel = UIWindowLevelStatusBar+2.0f;
self.userInteractionEnabled = NO;
Setting the Statusbar userInteractionEnabled property to NO will ensure that your scroll views will scroll to top when someone taps on this statusbar.

Resources