Custom navbar sizing from iphone 5 to iphone 6 - ios

I have a custom tab bar that was optimized for the iphone 5.
UINavigationBar *myBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
[self.view addSubview:myBar];
When it on the iphone 6, part of it is cut off of course because it only goes to 320 pixels.
How do i fix this? Is there a way to check which iphone its being ran on, and then run the pixel specified code? I plan on putting a background image on this navbar later so it must be centered.

In general it is a bad idea to hard code dimensions like this. This will break if you rotate, or if viewed on a screen with a different size than the original iPhone screen.
Many of these controls have a default size that you can use to your advantage. Instead of handing it a frame, consider just modifying the frame it gives you:
UINavigationBar *myBar = [[UINavigationBar alloc] init];
CGRect navBarFrame = myBar.frame;
navBarFrame.size.height // returns the right size for the current OS
navBarFrame.size.width = self.view.frame.size.width;
myBar.frame = navBarFrame;
This type of defensive coding is helpful in keeping your app laid out properly under many conditions, including when you embed this control into a parent view controller, or viewed on tomorrows larger-screened iOS devices.
All this said, are you sure you don't want a UIToolbar? Typically you don't ever create your own UINavigationBar, as that class is just used in UINavigationController for you.

UINavigationBar *myBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 50)];
[self.view addSubview:myBar];
may be this help you.

Related

The iOS navigation Bar seems not suitable as I set

This is a custom Navigation bar,the height I set for it is 64,but it is more than 64 as you can see.I don't how it is happened.
Here is my code:
navigationView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, 64)];
navigationView.userInteractionEnabled=YES;
navigationView.backgroundColor=[UIColor whiteColor];
navigationView.alpha=0.85;
[self.view insertSubview:navigationView atIndex:9999];
what confused me most is that it's shown OK on my iPhone5(ios8) but shown wrong on my iPhone 6(ios9)
I had solved this problem by myself.The solution is:New a launch screen of the project.That's the way.Then the status bar would recover to it's correct face.

iphone 6 banner messing up after adding iphone 6 launch screen

So in my app i have my launch screen/splash screen set for the 4, 4s, 5, and 6+....basically all the splash screens except for the one for the 6.
Everything looks great. background images for the toolbar are properly scaled and everything. however on the iphone 6 its status bar is black even though its set in the plist to white and i believe it has to do with the missing iphone splash screen.
So I add in the launch screen for the 6 to correct this right? Awesome it works! but now my background image for the 6 doesnt get scaled properly and its left at the iphone 5 #2x size with a repeated edge.
How do i correct this?
Heres the code im using to implement the background image:
-(void)viewWillAppear:(BOOL)animated{
UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64);
[toolbar setBackgroundImage:[UIImage imageNamed:#"Missions.png"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
[self.view addSubview:toolbar];
}
and here are my image names:
missions.png (320x64)
missions#2x.png (640x128)
missions#3x.png (1242x195)
Notice i dont really have an image for the iphone 6 (750xwhatever). thats because i dont see how i could fit it in the naming convention with how the code runs it. wish it were as easy as missions#2.5x.png.....
why don't you just paste this into your UIViewController subclass
-(UIStatusBarStyle)preferredStatusBarStyle{
return UIStatusBarStyleLightContent;
}
and go into your info.plist and add the key UIViewControllerBasedStatusBarAppearance and set it to YES.

iOS 8 Transparent View with non-transparent subviews

I want to place non transparent content on top of a UIImageView. So I have a view with a transparent background which contains all the content. And I've placed it on top of imageview. I've set this view's background color to clearColor but still I can not get the desired effect.
This was clearly possible with iOS 6 SDK , but now I'm trying with iOS 8 SDK which does not give me what I want.
Check this app's screenshot
I'm trying to achieve similar effect (a semi transparent view) with iOS 8 SDK. However I need to set opacity very low value which makes the view almost transparent. All I could achieve was very little transparency (something like alpha 0.9 even I set 0 ).
This can be achieved by Using UIVisualEffect and UIVisualEffectView which are available from iOS 8 on.
This will provide a transparent view, as you said, without affecting the subviews.
Try this
UIVisualEffectView *blurredView =[[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
[blurredView addSubview:self.mySubView];
and you can add your subviews and image views as well.
Add your required views in the order, similar to the below code...
UIImageView* bottomImgView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
bottomImgView.image=[UIImage imageNamed:#"yourImg.png"];
[self.view bottomImgView];
UIView* topTransparentView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
topTransparentView.backgroundColor = [UIColor clearColor];
[bottomImgView addSubview:topTransparentView];
UIView* subView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
subView.backgroundColor = [UIColor blueColor];
[topTransparentView addSubview:subView];

Bar translucency gone in iOS 7.0.3

Compare the two screenshots:
Done on iOS 7.0 simulator
And the one done on iOS 7.0.3 iPhone 4S:
Same code here and there and same stuff! Any idea why the translucency is gone on the real device?
I have this code to simulate it (I know it's probably awkward and not right but that's how it is):
topMenuView = [[UIView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, 0, self.view.frame.size.width, TOP_BAR_ORIG_HEIGHT)];
topMenuView.clipsToBounds = YES;
UIToolbar *topMenuViewBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, -4, self.view.frame.size.width, TOP_BAR_ORIG_HEIGHT + 4)];
topMenuViewBar.barStyle = UIBarStyleDefault;
topMenuViewBar.barTintColor = [BSFunctions getColorFromHex:#"1ea6ff"];
const CGFloat statusBarHeight = 20;
UIView *underlayView = [[UIView alloc] initWithFrame:CGRectMake(0, -statusBarHeight, topMenuViewBar.frame.size.width, topMenuViewBar.frame.size.height + statusBarHeight)];
[underlayView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
[underlayView setBackgroundColor:[BSFunctions getColorFromHex:#"1ea6ff"]];
[underlayView setAlpha:0.36f];
[topMenuViewBar insertSubview:underlayView atIndex:1];
UIView *underlayView2 = [[UIView alloc] initWithFrame:CGRectMake(0, -statusBarHeight, topMenuViewBar.frame.size.width, topMenuViewBar.frame.size.height + statusBarHeight)];
[underlayView2 setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
[underlayView2 setBackgroundColor:[BSFunctions getColorFromHex:#"0291ff"]];
[underlayView2 setAlpha:0.36f];
[topMenuViewBar insertSubview:underlayView2 atIndex:2];
[topMenuView addSubview:topMenuViewBar];
[self.view addSubview:topMenuView];
The main point is it used to work before on the device! But after iOS 7.0.3 came out it changed. I'm noticing the same behavior in Facebook and Fitocracy iOS apps.
UPDATE
On Xcode 5.0.1 with iOS 7.0.3 simulator we have this (which is different from the first image on iOS 7.0 simulator as you can see):
OK, so after having played around with the colours a bit more, I managed to get a similar sort of appearance with the blur!
Previously, I was setting a barTintColor on the navigation bar appearance which had the following values:
R:17
G:63
B:95
A:1
This was fine in iOS < 7.0.3, and the output color in the nav bar (with the blur effect) was actually:
R:62
G:89
B:109
Since iOS 7.0.3, the barTintColor seems to take into account the alpha value of the color we set. This meant that the nav bar was actually outputting a solid color 17,63,95, and there was no blur effect.
The key to getting the blur effect back is setting an alpha < 1 in the barTintColor.
After lots of guess work and trying different RGB values, I managed to get the exact same RGB output from the nav (and tab) bar, using the following RGBA:
R:4.5
G:61.6
B:98
A:0.65
It does not look like there's a magic ratio to apply to the previous color to obtain the new one.
Anyway I've actually rejected the binary that got approved this afternoon, and have re-submitted with these new values so that user's don't get an ugly app :)
Hope this helps.

Make the iPhone Screen Dim

I have managed to ensure that the iPhone doesn't auto-lock using:
[[ UIApplication sharedApplication ] setIdleTimerDisabled: YES ];
But how do I make the screen dim after a certain amount of time?
Thanks...
EDIT:
Think I've found a solution myself:
Use this method in your view controller to dim by adding a black view with 50% alpha. Make sure to set userInteractionEnabled = NO to pass events to underlying views.
- (IBAction)dim:(id)sender {
UIView *dimView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
dimView.backgroundColor = [UIColor blackColor];
dimView.alpha = 0.5f;
dimView.userInteractionEnabled = NO;
[self.view addSubview:dimView];
}
or, could risk it (but Apple may reject on submission):
[(id)[UIApplication sharedApplication] setBacklightLevel:1.0f];
That's a private API and shouldn't be used though...
As of iOS 5 there is a public API in the UIScreen class. It has a brightness property that can be set. For those instances where you may want to go dimmer than the actual backlight allows, there is a wantsSoftwareDimming property that will automatically place a translucent layer that will give the appearance of being more dim than can be done in hardware. This is very similar to the method you came up with with the translucent UIView. It should be noted that using your solution or the software dimming API should not be used with many animations since you will pay a performance penalty with all the alpha blending.
See UIScreen Class Reference
in iOS 7 and later:
yourViewController.view.tintAdjustmentMode = UIViewTintAdjustmentModeDimmed;

Resources