iOS 7 status bar background - objective-c

I have some view controllers with a toolbar at top and look like this
How I can fill the status bar background to match the toolbar background so its match the new iOS 7 style?

you need to add a subview in app delegate and change the color to your liking. here is a sample of the code that sits in applicationdidfinishLaunchingWithOptions
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// Override point for customization after application launch.
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
UIView *addStatusBar = [[UIView alloc] init];
addStatusBar.frame = CGRectMake(0, 0, 320, 20);
//change this to match your navigation bar or view color or tool bar
//You can also use addStatusBar.backgroundColor = [UIColor BlueColor]; or any other color
addStatusBar.backgroundColor = [UIColor colorWithRed:0.973/255. green:0.973/255. blue:0.973/255. alpha:1];
[self.window.rootViewController.view addSubview:addStatusBar];
}
return YES;
}
take a look at the comments. you can use any type of color in addStatusBar.backGroundColor to match the color you need. note that the color with red here is just producing a Black background, change that to whatever you need. for dark grey replace that with the following code:
addStatusBar.backgroundColor = [UIColor colorWithRed:85.0/255.0 green:85.0/255.0 blue:85.0/255.0 alpha:1];
edit:
for changing the status bar color in individual views you simply plug in the following code ( which will access the view you want to change) in ViewDidLoad method right after [super viewDidLoad]; that should change the color of the status bar in just that view you place the code in.
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
UIView *addStatusBar = [[UIView alloc] init];
addStatusBar.frame = CGRectMake(0, 0, 320, 20);
addStatusBar.backgroundColor = [UIColor colorWithRed:0.973/255. green:0.973/255. blue:0.973/255. alpha:1];
[self.view addSubview:addStatusBar];
edit1:
in case of trying to add a subview on top of the nag bar in navigation controller, you have to twice the position of the subview, something like the following:
UIView *addStatusBar = [[UIView alloc] init];
addStatusBar.frame = CGRectMake(0, -20, 320, 20);
addStatusBar.backgroundColor = [UIColor colorWithRed:127.0/255. green:0.0/255. blue:127.0/255. alpha:1];
[self.view addSubview:addStatusBar];
[self.navigationController.navigationBar addSubview:addStatusBar];
and also you need to add your subview as yet the subview of the navigation controllers' navigation bar. i set the background color of the subview to purple but you can change that.

Related

Display issue with customized UINavigationBar

I'm currently encountering an issue with the customized navigation bar of my app, especially on iPad Pro and iPad.
I've extended UINavigationBar to create a personalized top bar.
I'm only using Objectiv-C, and here is the initialization function :
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:(NSCoder *)aDecoder];
if (self)
{
[self setTranslucent:FALSE];
UIImageView *mylogo = [[UIImageView alloc] initWithImage:[self imageWithImage:[UIImage imageNamed:#"minilogo.png"] scaledToSize:CGSizeMake(200, 25)]];
[self.topItem setTitleView:mylogo];
menuButton = [UIButton buttonWithType:UIButtonTypeCustom];
menuButton.frame = CGRectMake(0, 0, 44, 44);
[menuButton setImage:[UIImage imageNamed:#"menuButton.png"] forState:UIControlStateNormal];
[menuButton setContentMode:UIViewContentModeCenter];
CALayer *menuBorder = [CALayer layer];
[menuBorder setFrame:CGRectMake(43, 0, 1, 44)];
[menuBorder setBackgroundColor:[[UIColor blackColor] CGColor]];
[menuButton.layer addSublayer:menuBorder];
[self.topItem setLeftBarButtonItem: [[UIBarButtonItem alloc] initWithCustomView:menuButton]];
searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
searchButton.frame = CGRectMake(0, 0, 44, 44);
UIImage *searchImage = [self imageWithImage:[UIImage imageNamed:#"boutonRecherche.png"] scaledToSize:CGSizeMake(44, 44)];
[searchButton setBackgroundImage:searchImage forState:UIControlStateNormal];
[searchButton setContentMode:UIViewContentModeCenter];
CALayer *searchBorder = [CALayer layer];
[searchBorder setFrame:CGRectMake(0, 0, 1, 44)];
[searchBorder setBackgroundColor:[[UIColor blackColor] CGColor]];
[searchButton.layer addSublayer:searchBorder];
[self.topItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:searchButton]];
if (#available(iOS 15.0, *))
{
UINavigationBarAppearance *navBarApparance = [[UINavigationBarAppearance alloc] init];
[navBarApparance setBackgroundImage:[UIImage imageNamed:#"fd_entete.png"]];
[self setStandardAppearance:navBarApparance];
[self setCompactAppearance:navBarApparance];
[self setScrollEdgeAppearance:navBarApparance];
}
else
{
[self setOpaque:YES];
[self setBackgroundImage:[UIImage imageNamed:#"fd_entete.png"] forBarMetrics:UIBarMetricsDefault];
}
}
return self;
}
My navigation bar is added to the view using the storyboard and I specifying its class name in the identity inspector tab.
I've set layout constraints so the content of my view is right under this navigation bar.
Here is a sample with a simple controller having the navigation bar and a webview beneath :
The issue I'm having only concerns iPads. Here is a screenshot of how it looks like on an iPad pro :
(the grey rect is just to illustrate that I've shorten the screenshot)
But surprisingly, the navigation bar render correctly if the view beneath is a UITableView, but it still have the border issue for the buttons - understand here that the button border is missing some pixels on top and bottom :
(it is also how the nav bar looks like on an iPad for any view controller)
In comparison, I don't have the issue on iPhones :
Any help is appreciated.

iOS11 Unable to change navigation bar height in UIViewController

I am creating navigation bar programmatically in my UIViewController in viewWillAppear. After I updated my iOS to iOS 11, the height remains constant and is not changing now. I checked all answers related to it on Stack but those answers are for UIView but not for UIViewController.
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
// Do any additional setup after loading the view.
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, 50)];
navBar.barTintColor = [UIColor colorWithRed:160.0/225 green:72.0/225 blue:178.0/225 alpha:1.0];
[self.view addSubview:navBar];
}
Thanks for help!

Use color with alpha fill NSView?

I write a window and two views,
set background color of window to blue,
and draw a image in view1,
and fill view2 with a color with alpha,like this:
//AppDelegate
[self.window setLevel:(NSStatusWindowLevel + 2)];
TestView *view1 = [[TestView alloc] initWithFrame:NSMakeRect(0, 0, self.window.frame.size.width, self.window.frame.size.height)];
TestView2 *view2 = [[TestView2 alloc] initWithFrame:NSMakeRect(0, 0, self.window.frame.size.width, self.window.frame.size.height)];
[self.window setContentView:view1];
[self.window.contentView addSubview:view2];
//View1
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
// Drawing code here.
NSImage *image = [NSImage imageNamed:#"testImage"];
[image drawInRect:dirtyRect];
}
//view2
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
// Drawing code here.
[[[NSColor redColor] colorWithAlphaComponent:0.5] set];
NSRectFill(dirtyRect);
}
But when the app run, I found I the backgroundcolor of window and image of View1 did not show, only the view2 show, and it is transparent,through it can see the desktop.
If I did not add subview view2, the app runs correctly, the blue background color and the image.
Does anybody know why, really thanks for the help, I will wait on line.
I change the NSRectFill to
NSRectFillUsingOperation(dirtyRect, NSCompositeSourceOver);
Then it turns right.

Root view controller is built in code - Wont resize for iPad

Im trying to convert my iphone app to ipad, all good until my root view controller. Its been built in code and no matter what I do I cant resize it/centre it. I have no nib for my Home page. Autoresize masks doesn't do anything, adding an iPad nib does nothing despite best efforts but continue to present the iphone screen in top left, and using if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) loads a a blank black iPad screen. I really dont know what to to now. If I set my screen elements to initWithFrame:[[UIScreen mainScreen] bounds]]; the artwork is distorted as its overstretched.
// Create the main view
UIView *mainView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
mainView.backgroundColor = [UIColor blackColor];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
UIImageView *textureBg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 768.0f, 1024.0f)];
textureBg.image = [UIImage imageNamed:#"BgTexture"];
[textureBg addSubview:textureBg];
}
else {
// Add textured background
UIImageView *textureBg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320.0f, 1024.0f)];
textureBg.image = [UIImage imageNamed:#"BgTexture"];
[textureBg addSubview:textureBg];
}
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
UIImageView *clipBg = [[UIImageView alloc] initWithFrame:CGRectMake(7.0f, 6.0f, 605.0f, 865.0f )];
clipBg.image = [UIImage imageNamed:#"BgHomeNew2"];
clipBg.userInteractionEnabled = YES;
}
else {
// Add clipboard background
UIImageView *clipBg = [[UIImageView alloc] initWithFrame:CGRectMake(7.0f, 6.0f, 305.0f, 465.0f )];
clipBg.image = [UIImage imageNamed:#"BgHomeNew2"];
clipBg.userInteractionEnabled = YES;
//[clipBg setAutoresizingMask:(UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin )];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
// Add about button to clipbg
UIButton *aboutButton = [UIButton buttonWithType:UIButtonTypeCustom];
aboutButton.frame = CGRectMake(240.0f, 90.0f, 31.0f, 33.0f);
[aboutButton setImage:[UIImage imageNamed:#"LabelButton"] forState:UIControlStateNormal];
[aboutButton addTarget:self action:#selector(aboutButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[aboutButton addSubview:aboutButton];
}
else {
// Add about button to clipbg
UIButton *aboutButton = [UIButton buttonWithType:UIButtonTypeCustom];
aboutButton.frame = CGRectMake(240.0f, 90.0f, 31.0f, 33.0f);
[aboutButton setImage:[UIImage imageNamed:#"LabelButton"] forState:UIControlStateNormal];
[aboutButton addTarget:self action:#selector(aboutButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[aboutButton addSubview:aboutButton];
}
How do I make an ipad view, for a universal app, where the root view controller is built in code? Im even open to the idea of simply centering the iphone screen in the middle of the ipad screen but as autoresing wont work its just stuck in the top left. This would be 2 mins in IB!
To summarise why wont the following code move the image to the center of the iPad screen?
UIImageView *clipBg = [[UIImageView alloc] initWithFrame:CGRectMake(7.0f, 6.0f, 305.0f, 465.0f )];
clipBg.image = [UIImage imageNamed:#"BgHomeNew2"];
clipBg.userInteractionEnabled = YES;
[clipBg setAutoresizingMask:(UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin)];
EDIT
Really dont know what im missing here, been on this a whole day now, Cant seem to add a xib retrospectively to an iphone screen built in code, so I will settle for centring objects in the middle of the screen when ipad is used rather than stuck in the top left all the time. That dosent work either.
Ok sorted it
Ive finally maged to add xibs now so I can adjust as necessary, I couldn't initialise the xibs before because I had missed this line of code in my rootview contoller
- (id)init
{
[[NSBundle mainBundle] loadNibNamed:#"ICHomeController" owner:self options:nil];

How to get UIView to overlay UIScrollView as a footer

I have a UIScrollView with an image that needs to be scalable. I want to have a "footer" with a black (opaque) background and white text. I wanted to have it be fixed as a footer. It will be opaque so you can see the image behind it.
I created a containing UIView for the scrollview and footer. I can get the scrollview to be smaller than the app frame and have a footer at the bottom filling in the extra space, but obviously I can't see the image behind the footer.
I also tried putting the UIScrollView and UIView (footer) inside the container and positioning them accordingly, but in this case I can't even see the footer. Any ideas?
Code I've gotten so far (executed in viewDidLoad of view controller):
CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
UIView* view = [[UIView alloc] initWithFrame:appFrame];
view.backgroundColor = [UIColor blackColor];
CGRect scrollViewFrame = CGRectMake(0.0, 0.0, view.frame.size.width, view.frame.size.height - 100);
UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
[scrollView setCanCancelContentTouches:NO];
scrollView.clipsToBounds = YES;
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
[scrollView addSubview:imageView];
[scrollView setContentSize:CGSizeMake(imageView.frame.size.width, imageView.frame.size.height)];
scrollView.minimumZoomScale = 0.7;
scrollView.maximumZoomScale = 5;
scrollView.delegate = self;
[scrollView setScrollEnabled:YES];
UIView* textView = [[UIView alloc] initWithFrame:CGRectMake(0.0, scrollViewFrame.size.height, appFrame.size.width, 100)];
// Red for testing purposes
textView.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.5];
[view addSubview:scrollView];
[view addSubview:textView];
self.view = view;
I assume that with "image" you mean that there is an UIImage in the scroll view, and that you want to see it through a half transparent footer view.
In this case you should not put the views beneath each other, but the footer view on top of the scroll view. You can insure it is on top by calling bringSubviewToFront or using [containerView insertSubview:footer atIndex:0]; instead of addSubview:.
Another caveat might be that you call your view view. There might be some unpredictable behavior about which view it is, so perhaps it is better to call it something else and then assign it to self.view.
I have plenty of UIScrollViews with a small non-scroll view at the bottom or the top. In most cases I don't have any overlap, and they just sit side-by-side. I create them all in InterfaceBuilder, and assign them to IBOutlets.