I have written the following coding ...
if ([[[UIDevice currentDevice] systemVersion] floatValue] > 4.9) {
//iOS 5
UIImage *toolBarIMG = [UIImage imageNamed: #"header.png"];
if ([self.navigationController.navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)]) {
[self.navigationController.navigationBar setBackgroundImage:toolBarIMG
forBarMetrics:0];
} else {
//iOS 4
[self.navigationController.navigationBar insertSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"header.png"]]
atIndex:0];
}
Image is shown in the navigation controller but there are some pixels gap in starting and ending of the image, its showing black image. How to set image so that black color should remove. You can see pink round, i want to remove those black color..
thanks in advance.
This is how you set an image to the navigation bar
UINavigationBar *navBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed:#"header.png"];
[navBar setBackgroundImage:image];
And put this code in the below method
- (void)viewWillAppear:(BOOL)animated
Set Your BarMetrics to Default
if ([[[UIDevice currentDevice] systemVersion] floatValue] > 4.9) {
//iOS 5
UIImage *toolBarIMG = [UIImage imageNamed: #"header.png"];
if ([self.navigationController.navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)]) {
[self.navigationController.navigationBar setBackgroundImage:toolBarIMG
forBarMetrics:UIBarMetricsDefault];
} else {
//iOS 4
[self.navigationController.navigationBar insertSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"header.png"]]
atIndex:0];
Related
I am trying to place an image as a background for viewcontroller in an ipad application. I set this using
UIImage *background = [UIImage imageNamed: #"Pilot#2x~ipad.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: background];
imageView.frame = self.view.bounds;
[self.view addSubview: imageView];
[self.view sendSubviewToBack:imageView];
This works fine for portrait mode, but when I rotate it to landscape mode there's a blank space where the picture doesn't fill. My idea based on other similar posts was to have a new picture that is called when the rotation get's changed and I tried this
{ if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
if((self.interfaceOrientation == UIDeviceOrientationLandscapeLeft) || (self.interfaceOrientation == UIDeviceOrientationLandscapeRight)){
UIImage *background = [UIImage imageNamed: #"Pilot#2x~ipad.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: background];
imageView.frame = self.view.bounds;
[self.view addSubview: imageView];
[self.view sendSubviewToBack:imageView];
} else if((self.interfaceOrientation == UIDeviceOrientationPortrait) || (self.interfaceOrientation == UIDeviceOrientationPortraitUpsideDown)){
UIImage *background = [UIImage imageNamed: #"PilotLandscape#2x~ipad.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: background];
imageView.frame = self.view.bounds;
[self.view addSubview: imageView];
[self.view sendSubviewToBack:imageView];
}}
Which resulted in the same thing. Any ideas on how to fix this would be appreciated!
Check view's autosizing settings, try to NSLog view's coordinates, pay attention to uiimageview's UIViewContentModeScale property. Why don't you simply add imageview to your storyboard?
Working Code :
#interface SOViewController (){
UIImageView *imageView;
}
#end
-(void)initialOrientationHandling
{
//[self.view removeFromSuperview];
if ([[UIDevice currentDevice]orientation] == UIDeviceOrientationLandscapeLeft ||
[[UIDevice currentDevice]orientation] == UIDeviceOrientationLandscapeRight){
imageView.frame = self.view.bounds;
[self.view addSubview: imageView];
[self.view sendSubviewToBack:imageView];
}else if ([[UIDevice currentDevice]orientation] == UIDeviceOrientationPortrait ||
[[UIDevice currentDevice]orientation] == UIDeviceOrientationPortraitUpsideDown){
imageView.frame = self.view.bounds;
[self.view addSubview: imageView];
[self.view sendSubviewToBack:imageView];
}
else if([[UIDevice currentDevice]orientation]== UIDeviceOrientationFaceUp){
if(self.view.frame.size.height < 900){
}else{
}
}
}
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[self initialOrientationHandling];
}
Obviously the image will be distorted if not highly pixled. I recommand you use different for different rotations.
SWIFT:
First create two image, one is landscape view size and another for portrait.
Initialize imageview
var Imgview = UIImageView()
In viewWillAppear method , it change image when view load and also when rotate.
override func viewWillAppear(animated: Bool) {
Imgview.frame = self.view.frame
Imgview.contentMode = UIViewContentMode.ScaleAspectFill
Imgview.clipsToBounds = true
view.addSubview(Imgview)
// get notification while rotating
NSNotificationCenter.defaultCenter().addObserver(self, selector: "rotate", name: UIDeviceOrientationDidChangeNotification, object: nil
)
}
Function rotate for set image,it is called in notification center.
func rotate()
{
if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
{
Imgview.frame = self.view.frame
Imgview.image = UIImage(named: "landscapeSizeimage.png")
self.view.sendSubviewToBack(Imgview)
}
if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
{
Imgview.frame = self.view.frame
Imgview.image = UIImage(named: "PortraitSizeimage.png")
self.view.sendSubviewToBack(Imgview)
}}
This worked for me.Hope it helps someone.And i dont know may be it create more imageviews so instead of bringtosubview we can remove imageview as your wish .
I'm trying to show a badge on some UITabBarItems that are configured with an image with UIImageRenderingModeAlwaysOriginal. I'm doing this because the desired effect is something like a raised UITabBarItem a-la Instagram. This is the code I'm using to test this (it's in a UITabBarController category):
- (void) replaceImageForTab:(int)itemIndex
defaultImage:(UIImage *)defaultImage
selectedImage:(UIImage *)selectedImage
title:(NSString *)title
{
NSArray *viewControllers = [self viewControllers];
UIViewController *vc = [viewControllers objectAtIndex:itemIndex];
[vc.tabBarItem setImage:[defaultImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[vc.tabBarItem setSelectedImage:[selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[vc.tabBarItem setTitle:title];
[vc.tabBarItem setBadgeValue:#"1"];
}
However, the badges images are shown clipped, like shown in here:
I think is because the badge is being added within the UITabBar's UIView, and it is clipping its contents. But I can't modify that value since that's a private property. What other options do I have here?
I haven't found a way to change the offset in the stock badges, so I had to roll out my own solution. It's not pretty, but it works (this is, again, on a UITabBarController category):
- (void) v_setBadgeValue:(NSString *)badgeValue inTab:(NSInteger)tabIndex
{
NSArray *viewControllers = [self viewControllers];
UIViewController *vc = [viewControllers objectAtIndex:tabIndex];
UITabBarItem *tabBarItem = vc.tabBarItem;
UIImage *normalImage = tabBarItem.image;
UIImage *selectedImage = tabBarItem.selectedImage;
UIImage *badgedNormalImage = [[self class] addBadgeWithValue:badgeValue toImage:normalImage];
UIImage *badgedSelectedImage = [[self class] addBadgeWithValue:badgeValue toImage:selectedImage];
[tabBarItem setImage:[badgedNormalImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem setSelectedImage:[badgedSelectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
}
+ (UIImage *) addBadgeWithValue:(NSString *)badgeValue toImage:(UIImage *)image
{
//Here you have to create your badge view. Let's just use a red square for now
UIView *badgeView = [[UIView alloc] initWithFrame:CGRectMake(16, 3, 14, 14)];
[badgeView setBackgroundColor:[UIColor redColor]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[imageView addSubview:badgeView];
UIGraphicsBeginImageContextWithOptions((imageView.bounds.size), NO, [[UIScreen mainScreen] scale]);
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return viewImage;
}
Now you're in complete control of the badges offset and position, that's important if you, like me, have a very strange UITabBar to work with. This is the result:
I have those 2 issues in iOS7 that I can't figure out.
The first issue is that the TableView rows goes beneath the Status Bar, how can I disable it, or to make the section header of the table view to be under the status bar all the time?
The second issue that I have is on the navigation controller the status bar seems to be black with black background and I don't know how to fix it, the view controllers background is white, but the status bar reminds black and I don't know why.
UPDATE:
Still no answer.
I've got the answer in another post, please check it everyone who has problems with iOS6-7 and StatusBar:
UIStatusBar as in Facebook new iOS7 application
Enjoy!
I think you need to do following code.
You will have to check condition for ios7.
You will need these two delegate methods for tableview.
Check my code below.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
int versionValue=[currSysVer intValue];
if(versionValue>=7)
{
return 60;
}
return 0;
}
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 60)];
[headerView setBackgroundColor:[UIColor clearColor]];
return headerView;
}
Hope this helps you.
use these lines of code
if ([self respondsToSelector:#selector(setNeedsStatusBarAppearanceUpdate)]) {
// iOS 7
[self prefersStatusBarHidden];
[self performSelector:#selector(setNeedsStatusBarAppearanceUpdate)];
} else {
// iOS 6
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
}
and
- (BOOL)prefersStatusBarHidden
{
return YES;
}
Answer for your second issue!
Put in your didFinishLaunchingWithOptions:
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
[self customizeios7];
} else {
[self customizeios6];
}
And make these 2 voids:
-(void)customizeios6 {
UIImage *navBackgroundImage = [UIImage imageNamed:#"navigationbar-ios6"];
[[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];
}
-(void)customizeios7 {
// SET STATUSBAR TEXT WHITE
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
// SET NAVIGATION IMAGE
UIImage *navBackgroundImage = [UIImage imageNamed:#"navigationbar-ios7"];
[[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];
}
The image dimensions for your UINavigationBar:
For iOS 6:
640px / 88px
For iOS 7:
640px / 128px
Any idea how to shift the navbar view so I have the expected padding? It looks like the popover frame is overlapped on the nav bar's top, left and right side.
This is my nav controller in a popover controller derived from the app's root splitViewController. It's an issue on < iOS 5.1
The solution is to remove the custom UINavigationBar styling when in a UIPopoverController. I'll post the implementation when it's ready.
Update
So this is a funny one. iOS 4.x and iOS 5.0 use a real, genuine popover, that is the floating popover. Custom navigation bars don't work in this popover (navBar setBackgroundImage in iOS 5.0 and the category drawRect override in iOS 4.x).
However, 5.1 uses more of a "slideover," where the custom navbar works nicely. It's a much cleaner design actually.
Anyway, my point is that now I need to remove the custom navBar formatting only when in portrait AND the OS is less than 5.1. Normally, you'd want to use respondsToSelector in lieu of manually figuring out an OS version. That doesn't work in this case.
Still working on the iOS 4.x fix, but here's iOS 5.0:
In my appDelete, where I set up the custom navBar:
//iOS 5.x:
if ([self.navigationController.navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)])
{
UIImage *image = [UIImage imageNamed:#"header.png"];
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
//iOS 4.x:
#implementation UINavigationBar (CustomBackground)
- (void)drawRect:(CGRect)rect
{
UIImage *image = [UIImage imageNamed:#"header.png"];
[image drawInRect:CGRectMake(0, 0, 320, 44)];
}
#end
I set up an observer in didFinishLaunching:
//start orientation notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(didRotate:)
name:#"UIDeviceOrientationDidChangeNotification"
object:nil];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
- (void) didRotate:(NSNotification *)notification
{
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version < 5.1)
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (UIDeviceOrientationIsPortrait(orientation))
{
if ([self.navigationController.navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)])
{
[self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
}
}
else
{
if ([self.navigationController.navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)])
{
UIImage *image = [UIImage imageNamed:#"header.png"];
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
}
}
}
I am having troubles after updating to Xcode 4.2.
Before I used the following codes for making custom navigation bar, but when i use iPhone 5.0 simulator, it fails whereas in iPhone 4.2 simulator it was ok.
May I know what is the problem and how can i fix this?
Many thanks
#implementation UINavigationBar (UINavigationBarCustomDraw)
- (void) drawRect:(CGRect)rect {
[self setTintColor:[UIColor colorWithRed:0.4f
green: 0.0f
blue:0.4f
alpha:1]];
if ([self.topItem.title length] > 0 && ![self.topItem.title isEqualToString:#""])
{
[[UIImage imageNamed:#"purple.jpg"] drawInRect:rect];
}
}
#end
If you need custom UINavigationbar with some image so you need to put this code in rootViewController that is first view of navigate stack (A > B > C , so you have to put this in A)
- (void)viewDidLoad
{
[super viewDidLoad];
[[UIDevice currentDevice] systemVersion];
if ([[[UIDevice currentDevice] systemVersion] floatValue] > 4.9) {
//iOS 5
UIImage *toolBarIMG = [UIImage imageNamed: #"purple.jpg"];
if ([self.navigationController.navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)]) {
[[UINavigationBar appearance] setBackgroundImage:toolBarIMG forBarMetrics:UIBarMetricsDefault];
}
} else {
//iOS 4
[self.navigationController.navigationBar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"purple.jpg"]] autorelease] atIndex:0];
}
}
}