Share Coding

Tutorials, Problems, Stuffs …

Category Archives: Objective-C

Show / Enable the status bar after adding a UIImagePickerController

The status bar will disappear after adding a uiimagepickercontroller to view. 
To show/enable the status bar normally on screen, 
we need to add the code to call the status bar:

- (void) viewDidAppear:(BOOL)animated {
  [super viewDidAppear:animated];
  [[UIApplication sharedApplication] setStatusBarHidden:NO animated:animated];
}
Advertisements

Hide the iPhone Status Bar on XCode 4

1. Project -> Target -> Info -> Right Click – Add row
Hide Status Bar 1

Read more of this post

Get local time and unix time by using NSDate and NSTimeZone on IOS

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *startDate = [dateFormatter dateFromString:@"2012-02-18 00:00:00"];
NSLog(@"%@", startDate);

The result is :
2012-02-17 16:00:00 +0000

 
We can see the time is not correct since the default timezone of NSDate is GMT +000(London), we must get the systemTimeZone and add interval to the date:

NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate:startDate];
startDate = [startDate addTimeInterval:interval];
NSLog(@"%@", startDate);

The result is :
2012-02-18 00:00:00 +0000

 
Read more of this post

Make Background Image Fitting to the UIView

1.             Too Small                                    Too Large                                What we want
Too SmallToo LargeFit

2. To make a centered and stretched image:

UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:@"image.png"] drawInRect:self.view.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

self.view.backgroundColor = [UIColor colorWithPatternImage:image];

Objective C – Basic UIView with navigation controller

This is a changing views solution:

[self presentModalViewController:view animated:YES];

 

In some situations that wanting to use Navigation Controller’s changing page animation effect:

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
	// IOS 5.x
	self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
	self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
	UINavigationController *nvc =[[UINavigationController alloc] initWithRootViewController:viewController];
	nvc.navigationBar.barStyle = UIBarStyleBlack;
	[window addSubview:nvc.view];
	[self.window makeKeyAndVisible];
	return YES;

	/* IOS 4.x
	UINavigationController *nvc =[[UINavigationController alloc] initWithRootViewController:viewController];
	nvc.navigationBar.barStyle = UIBarStyleBlack;
	[window addSubview:nvc.view];
	[self.window makeKeyAndVisible];
	return YES;
	*/
}

 
Read more of this post