SafeArea in iOS 11: how add view on main screen with custom navigationBar without safeArea from code in objective-c - objective-c

In my project on Objective-C mainScreen with Custom NavigationBar (created from code):
mainNavigationController = [[NavigationController alloc] initWithRootViewController:mainMenuViewController];
mainNavigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
mainNavigationController.delegate = self;
mainNavigationController.navigationBar.tintColor = [UIColor whiteColor];
UIWindow* window = self.window;
window.backgroundColor = [UIColor whiteColor];
window.rootViewController = mainNavigationController;
[window makeKeyAndVisible];
I change self.view like size [UIScreen mainScreen].applicationFrame]:
UIView* mainView = [[[MainViewControllerView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame] autorelease];
self.view = mainView;
In mainScreenView I add scrollView:
scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
scrollView.autoresizesSubviews = YES;
scrollView.autoresizingMask = UIViewAutoresizingNone;
scrollView.backgroundColor = [UIColor clearColor];
scrollView.bounces = NO;
And add some subView inside scrollView:
[scrollView addSubview:myLogoView];
[scrollView addSubview:littleScrollView];
[scrollView addSubview:firstButton];
[scrollView addSubview:secondButton];
[scrollView addSubview:thirdButton];
[scrollView addSubview:fouthButton];
[scrollView addSubview:fifthButton];
[scrollView addSubview:sixthButton];
[scrollView addSubview:seventhButton];
[scrollView addSubview:activityIndicatorView];
https://www.dropbox.com/s/ttrkolgnch80kr9/safeArea.png?dl=0
If use XCODE 8 and iOS <= 10, all view place correct on mainScreen (myLogoView place ignore size of custom NavigationBar and have coordinate Y == 20.0 in absolute value coordinates of device screen),
but if use XCode 9 and iOS 11 myLogoView have place under my custom navigationBar (height == 44) myLogoView Y == 64.0 in absolute value coordinates of device screen, in iOS 10 (under xCode9) all working good - added view on mainScreen placed in start coordinates of screen and ignore height of custom NavigationBar.
In swift and storyboard I know how it fixed in iOS11 easy remove safeArea top line, but how remove safeArea from code in Objective-C.
How fix this trouble?

Safe area in iOS 11 is replacement of top & bottom layout and apple provide more detail on this like below :
The layout guide representing the portion of your view that is
unobscured by bars and other content. In iOS 11, Apple is deprecating
the top and bottom layout guides and replacing them with a single safe
area layout guide.
So base on this now top & bottom layout replaced with single safe area but agin if you are not using default NavigationBar so you can disable safe area in storyboard like below :
You can Uncheck safe area and it will revert to top & bottom layout.
Ref Before & After uncheck safe area:
Before or default :
Uncheck Safe area or old behavior :
Hope this will help to understand new iOS 11 update related to top & bottom layout and change your code as per this layout change.
Also here is one good blog to explain more on Safe area layout : Safe area

Related

Setting a view using inset of bounds

I started up an Xcode project using the 'single view' application template and added two lines to the template-created ViewController class in viewDidLoad:
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectInset(self.view.bounds, 10, 10)];
[self.view addSubview:textView];
When I run this in the 3.5 inch iPhone simulator, the textview extends off the bottom of the screen. I intended for it to be placed in the center of the screen with a 10 point border.
Am I missing something basic? It seems to work fine in the 4" simulator. Is it a simulator bug?
It's not a bug in the simulator.
The Xcode template includes a xib, and in that xib is the view controller's top-level view (self.view), and that view is sized for the iPhone 5 screen.
When the system sends viewDidLoad to your view controller, it hasn't yet resized that view for the screen size of the current device. So your text view's frame is based on the size of an iPhone 5 screen.
You can fix this by setting the autoresizing flags on your view:
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectInset(self.view.bounds, 10, 10)];
[self.view addSubview:textView];
textView.autoresizingMask = UIViewAutoresizingFlexibleHeight
| UIViewAutoresizingFlexibleWidth;
Your xib is set up to use autolayout by default, so you can also have the system resize your text view by setting constraints between your text view and your top-level view:
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectInset(self.view.bounds, 10, 10)];
[self.view addSubview:textView];
textView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:#"H:|-10-[textView]-10-|" options:0 metrics:nil
views:NSDictionaryOfVariableBindings(textView)]];
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:#"V:|-10-[textView]-10-|" options:0 metrics:nil
views:NSDictionaryOfVariableBindings(textView)]];
If you put your text view in the xib instead of creating it in code, you can use the xib editor (aka “Interface Builder” or “IB”) to set up the constraints. I highly recommend watching the autolayout videos from WWDC 2012:
Session 202 - Introduction to Auto Layout for iOS and OS X
Session 228 - Best Practices for Mastering Auto Layout
Session 232 - Auto Layout by Example

Visible buttons with transparent navigation bar

I've seen several apps that have completely transparent navigation bars but with visible buttons, I cant seem to find anything that wont make the button invisible as well. I'm sure they were using UINavigationController for the navbar because it had the same animations with fade and what not.
Im currently using this code in ViewDidLoad and ViewDidAppear to either hide or show the nav bar because it's not supposed to be on the first page-
[self.navigationController setNavigationBarHidden:NO animated:YES];
and this code for its transparency:
[self.navigationController.navigationBar setAlpha:0.0];
Create a subclass of UINavigationBar containing no methods except drawRect:. Put custom drawing code there if you need to, otherwise leave it empty (but implement it).
Next, set the UINavigationController's navigation bar to this subclass. Use initWithNavigationBarClass:toolBarClass: in code, or just change it in the Interface Builder if you're using storyboards/nibs (it's a subclass of your UINavigationController in the hierarchy at the side).
Finally, get a reference to your navigation bar so we can configure it using self.navigationController.navigationBar in the loadView of the contained view controller. Set the navigation bar's translucent to YES and backgroundColor to [UIColor clearColor]. Example below.
//CustomNavigationBar.h
#import <UIKit/UIKit.h>
#interface CustomNavigationBar : UINavigationBar
#end
//CustomNavigationBar.m
#import "CustomNavigationBar.h"
#implementation CustomNavigationBar
- (void)drawRect:(CGRect)rect {}
#end
//Put this in the implementation of the view controller displayed by the navigation controller
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.translucent = YES;
[self navigationController].navigationBar.backgroundColor = [UIColor clearColor];
}
Here's a screen shot of the result, mimicking Plague.
The blue border was drawn in drawRect: to show you that a UINavigationBar is there and not just a button and a label. I implemented sizeThatFits: in the subclass to make the bar taller. Both the button and label are UIView's containing the correct UI element that were placed in the bar as UIBarButtonItems. I embedded them in views first so that I could change their vertical alignment (otherwise they "stuck" to the bottom when I implemented sizeThatFits:).
self.navigationController.navigationBar.translucent = YES; // Setting this slides the view up, underneath the nav bar (otherwise it'll appear black)
const float colorMask[6] = {222, 255, 222, 255, 222, 255};
UIImage *img = [[UIImage alloc] init];
UIImage *maskedImage = [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(img.CGImage, colorMask)];
[self.navigationController.navigationBar setBackgroundImage:maskedImage forBarMetrics:UIBarMetricsDefault];
//remove shadow
[[UINavigationBar appearance] setShadowImage: [[UIImage alloc] init]];
To make the Navigation Bar transparent, use the below code:
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
self.navigationController.navigationBar.tintColor = [UIColor clearColor];
self.navigationController.navigationBar.translucent = YES;
After this set the background image of the Navigation Bar the same as the view behind it using the below property:
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"SAMPLE.jpg"] forBarMetrics:UIBarMetricsDefault];

In Cocoa-Touch Framework, is there any modal view in iPad like this?

Is there any model view like this on the iPad?
Yes, there is:
UIModalPresentationFormSheet
The width and height of the presented view are smaller than those of the screen and the view is centered on the screen. If the device is in a landscape orientation and the keyboard is visible, the position of the view is adjusted upward so that the view remains visible. All uncovered areas are dimmed to prevent the user from interacting with them.
For example:
UIViewController *viewController = [[UIViewController alloc] init];
viewController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:viewController animated:YES];
viewController.view.superview.frame = CGRectMake(0, 0, 540, 500); // this is important to do this after presentModalView
viewController.view.superview.center = self.view.superview.center;

UISplitViewController: remove divider line

When using UISplitViewController on the iPad there's a black vertical divider line between the root and detail view. Is there any way to remove this line?
Thanks
Excellent answer by #bteapot. I tested this and it works, even gets rid of the line between master/detail nav bars.
You can do this in storyboard by adding the "gutterWidth" key path and the value 0 to the USplitViewController runtime attributes.
Actuly I have some modification to answer of (Dylan)'s answer
in the appDelegate we need to add image in spliteview controller rather then window
self.splitViewController.view.opaque = NO;
imgView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:#"FullNavBar.png"]];
[imgView setFrame:CGRectMake(0, 0, 1024, 44)];
[[self.splitViewController view] insertSubview:imgView atIndex:0];
[[self.splitViewController view] setBackgroundColor:[UIColor clearColor]];
here self is object of AppDelegate.
now Apply the answer of this thread : iPhoneOS SDK - Remove Corner Rounding from views (iPad problem) answer by (abs)
edit in above post's answer is
-(void) fixRoundedSplitViewCorner {
[self explode:[[UIApplication sharedApplication] keyWindow] level:0];
}
-(void) explode:(id)aView level:(int)level
{
if ([aView isKindOfClass:[UIImageView class]]) {
UIImageView* roundedCornerImage = (UIImageView*)aView;
roundedCornerImage.hidden = YES;
}
if (level < 2) {
for (UIView *subview in [aView subviews]) {
[self explode:subview level:(level + 1)];
}
}
imgView.hidden = FALSE;
}
** make imgView.hidden to FALSE
declare imgView to the AppDelegate.h file**
and dont forget to call this
-(void)didRotateFromInterfaceOrientation:
UIInterfaceOrientation)fromInterfaceOrientation
{
[yourAppDelegate performSelector:#selector(fixRoundedSplitViewCorner)
withObject:NULL afterDelay:0];
}
chintan adatiya answer covers only the corners and the navigation bar, but I found an trick how to cover the line between the Master and the Detail view.
It is not nice but it works like a charm.
First create an image which is 1 px wide and 704 pixels high.
In the didFinishLaunchingWithOptions add the following code:
UIView *coverView = [[UIView alloc] initWithFrame:CGRectMake(320, 44, 1, 704)];
[coverView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"divider_cover.png"]]];
[splitViewController.view addSubview:coverView];
And done.
When you want a background image which is continues create 3 images:
Master: width: 320, height: 704
Detail: width: 703, height: 704
Divider:width: 1, height: 704
First post here, hi everyone.
I discovered how to do it accidentally... when I tried to find why I had LOST the divider line. Here's how to hide it, if you're still interested:
1) In your Detail (right-side) view, make sure you have a subview that spans the whole view.
2) Offset this subview view to (-1, 0).
3) Make sure that the Detail View has its "Clip Subviews" option unchecked.
Voilà, enjoy.
You can mostly get rid of it by setting another image behind it in the main window's views. This is from the app delegate didFinishLaunchingWithOptions
// Add the split view controller's view to the window and display.
splitViewController.view.opaque = NO;
splitViewController.view.backgroundColor = [UIColor clearColor];
[window addSubview:splitViewController.view];
[window insertSubview:bgImageView belowSubview:splitViewController.view];
[window makeKeyAndVisible];
But it still leaves two visual artifacts at the top and the bottom that appear to be custom drawn by the splitviewcontroller.
Interestingly, In the app that I'm working on I want a black background color for both views in the UISplitViewController. I'd like to change the color of the divider line to white (so that you can see it). Making both background colors black is one way to get rid of (make invisible) the dividing line but that's probably not a solution for most people.
Tested on iOS10 (probably will work on iOS9 too).
splitviewController.view.backgroundColor = UIColor.white
it removes divider. Apparently divider is just a gap between master and detail container.
I looked around for a while, and came to the conclusion that theres no way to do this, other than to create your own custom split view.
Try the MGSplitViewController by Matt Gammell
http://mattgemmell.com/2010/07/31/mgsplitviewcontroller-for-ipad
I may be late here, but I DO have a solution that works. It even works for the iOS 8+ splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModeAllVisible; and seamlessly slides in and out when you press the Full Screen toggle button.
Here is the trick :
first Subclass UISplitViewController.m
In the header add the follwing :
#property (strong, nonatomic) UIView *fakeNavBarBGView;
In the viewDidLoad method add the following code :
CGFloat fakeNavBarWidth = 321; // It is important to have it span the width of the master view + 1 because it will not move when the split view slides it's subviews (master and detail)
CGFloat navbarHeight = self.navigationController.navigationBar.frame.size.height + 20;
self.fakeNavBarBGView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, fakeNavBarWidth, navbarHeight)];
self.fakeNavBarBGView.backgroundColor = [UIColor redColor];
// Add Fake navbar to back of view
[self.view insertSubview:self.fakeNavBarBGView atIndex:0];
// SplitView Controller
UISplitViewController *splitViewController = self;
DetailViewController *detailVC = [navigationController.viewControllers lastObject];
detailVC.fakeNavBarSubView = self.fakeNavBarBGView;
detailVC.SVView = self.view;
In the DetailViewController.h add the following :
#property (strong, nonatomic) UIView *SVView;
#property (strong, nonatomic) UIView *fakeNavBarSubView;
Now here is the final trick : in the DetailViewController.m, add the following in the viewDidLoad method (called every time you click the Master table) :
[self.SVView sendSubviewToBack:self.fakeNavBarSubView];
[self.SVView bringSubviewToFront:self.view];
Run it and watch the magic ;-)
Private API (can cause App Store rejection):
[splitViewController setValue:#0.0 forKey:#"gutterWidth"];
I did this accidentally by setting the backgroundColor property of the first viewController's view - possibly to clearColor, I don't remember now.
UIManager.put("SplitPaneDivider.draggingColor", new Color(255, 255, 255, 0));

Iphone SDK - Multiple subviews and shouldAutorotateInterfaceOrientation

The iPhone Application Programming Guide shows an example labelled "Listing 2-1 Creating a window with views" (see below). This shows how to create and add two subviews to a window.
I am using a similar pattern=. This works correctly, both windows get displayed.
The problem I am having is to get it to recognize and do rotation. I have added the shouldAutorotateInterfaceOrientation methods to do a return YES. These are being seen. But only one of the views gets rotated.
More specifically the last view to be added gets rotated and the previous one does not. I can get either to rotate by having it as the second addsubview. But cannot get both to rotate. (Testing in the Iphone simulator.)
Any suggestions on what is needed to get both views to rotate correctly?
Here is Apples sample code.
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Create the window object and assign it to the
// window instance variable of the application delegate.
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
window.backgroundColor = [UIColor whiteColor];
// Create a simple red square
CGRect redFrame = CGRectMake(10, 10, 100, 100);
UIView *redView = [[UIView alloc] initWithFrame:redFrame];
redView.backgroundColor = [UIColor redColor];
// Create a simple blue square
CGRect blueFrame = CGRectMake(10, 150, 100, 100);
UIView *blueView = [[UIView alloc] initWithFrame:blueFrame];
blueView.backgroundColor = [UIColor blueColor];
// Add the square views to the window
[window addSubview:redView];
[window addSubview:blueView];
// Once added to the window, release the views to avoid the
// extra retain count on each of them.
[redView release];
[blueView release];
// Show the window.
[window makeKeyAndVisible];
}
This question was abandoned, sadly...
I would love to find the answer to that, as it causes alot of problems on the iPad SDK... adding more than 1 subview at a forced landscape mode, will only make one rotated view.
Causing alot of confusion, something is very wrong with the system.
~ Natan.