iPhone crash randomly when calling a camera? - ios

I have tested my app using instrument and it show the following leak.
When I double click on the CameraVC in the stack trace it refer to the following line into my code.
this happen when I call the camera, I call it using the following code:-
- (IBAction) getCamera
{
// set up our camera overlay view
// tool bar - handy if you want to be able to exit from the image picker...
UIToolbar *toolBar=[[UIToolbar alloc] initWithFrame:CGRectMake(0, 480-44, 320, 44)];
UIBarButtonItem *spaceItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil] autorelease];
UIBarButtonItem *spaceItem1 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil] autorelease];
UIBarButtonItem *cancelItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelPickingImag)] autorelease];
UIBarButtonItem *cameraItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:#selector(finishedAugmentedReality)] autorelease];
spaceItem.width = 2.0;
spaceItem1.width = 55.0;
NSArray *items=[NSArray arrayWithObjects:spaceItem,cancelItem,spaceItem1,cameraItem,nil];
[toolBar setItems:items];
// create the overlay view
overlayView=[[OverlayView alloc] initWithFrame:CGRectMake(0, 300, 320, 480-44)];
// important - it needs to be transparent so the camera preview shows through!
overlayView.opaque=NO;
overlayView.backgroundColor=[UIColor clearColor];
// parent view for our overlay
UIView *parentView=[[UIView alloc] initWithFrame:CGRectMake(0,0,320, 480)];
[parentView addSubview:overlayView];
[parentView addSubview:toolBar];
[parentView addSubview:lbl];
[parentView addSubview:overlayGraphicView];
// configure the image picker with our overlay view
//UIImagePickerController *picker=[[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera])
{
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
// hide the camera controls
imagePicker.showsCameraControls=NO;
imagePicker.delegate = self;
//imagePicker.allowsImageEditing = NO;
// and put our overlay view in
imagePicker.cameraOverlayView=parentView;
}
else
{
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = [NSArray arrayWithObjects:
(NSString *) kUTTypeImage,
nil];
imagePicker.allowsEditing = NO;
}
[self presentModalViewController:imagePicker animated:YES];
//Free memory
[imagePicker release];//,imagePicker =nil;
[parentView release], parentView=nil;
[overlayGraphicView release], overlayGraphicView= nil;
[lbl release], lbl=nil;
[overlayView release];//, overlayView =nil;
[toolBar release], toolBar=nil;
}
any help is highly appreciated
Thanks

parentView is being created with [alloc [init...]], meaning it's retained. Then you assign it to the cameraOverlayView property of imagePicker, meaning it gets retained again. You need to release it after you do that assignment.
(This is almost certainly the cause of the "leak", but would not cause a "crash". You say you're having a crash but don't describe that at all.)

Related

Navigationbar right button is not placed correctly

I implemented a UITabBarController with 4 items.
I set the initial ViewController from AppDelegate.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
HomeTabBarViewController* homeVC = [[HomeTabBarViewController alloc] init];
[self.window setRootViewController:homeVC];
[self.window makeKeyAndVisible];
return YES;
}
Here is HomeTabBarViewController ViewDidLoad Code:
UIViewController *view1 = [[UIViewController alloc] init];
[view1.view setBackgroundColor:[UIColor redColor]];
UIViewController *view2 = [[UIViewController alloc] init];
UINavigationController * centerNav=[[UINavigationController alloc]initWithRootViewController:view1];
HomePagingViewController * view3=(HomePagingViewController*)[storyboard instantiateViewControllerWithIdentifier:#"HomePagingViewController"];
UINavigationController * centerNav3=[[UINavigationController alloc]initWithRootViewController:view3];
UIViewController *view4 = [[UIViewController alloc] init];
UINavigationController * centerNav4=[[UINavigationController alloc]initWithRootViewController:view4];
[view2.view setBackgroundColor:[UIColor greenColor]];
[view3.view setBackgroundColor:[UIColor purpleColor]];
[view4.view setBackgroundColor:[UIColor grayColor]];
NSMutableArray *tabViewControllers = [[NSMutableArray alloc] init];
[tabViewControllers addObject:centerNav];
[tabViewControllers addObject:view2];
[tabViewControllers addObject:centerNav3];
[tabViewControllers addObject:centerNav4];
[self setViewControllers:tabViewControllers];
view1.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Home"
image:[UIImage imageNamed:#"home_tab_item.png"] selectedImage:[UIImage imageNamed:#"home_tab_item_active.png"]] ;
view2.tabBarItem =
[[UITabBarItem alloc] initWithTitle:#"Bookmark"
image:[UIImage imageNamed:#"bookmark_tab_item.png"] selectedImage:[UIImage imageNamed:#"bookmark_tab_item_active.png"]];
view3.tabBarItem =
[[UITabBarItem alloc] initWithTitle:#"Paper"
image:[UIImage imageNamed:#"paper_tab_item.png"] selectedImage:[UIImage imageNamed:#"paper_tab_item_active.png"]];
view4.tabBarItem =
[[UITabBarItem alloc] initWithTitle:#"More"
image:[UIImage imageNamed:#"menu_tab_item.png"] selectedImage:[UIImage imageNamed:#"menu_tab_item_active.png"]];
In HomePagingViewController ViewDidLoad I setUp a Navigation as in code
-(void)navigationSetUp{
self.navigationController.navigationBar.translucent=NO;
UIBarButtonItem * settingBtn=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"settings.png"] style:UIBarButtonItemStylePlain target:self action:#selector(menuHandler:)];
UIBarButtonItem * searchBtn=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"settings.png"] style:UIBarButtonItemStylePlain target:self action:#selector(menuHandler:)];
self.navigationItem.leftBarButtonItem = settingBtn;
self.navigationItem.rightBarButtonItem = searchBtn;
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"ie_New_logo.png"]];
}
After this when I changed the tab for HomePagingViewController the navigation right button not placed correctly there is no margin from right.
Refer Image :
Please Help.
I resolved the issue , there is no issue with the code . Actually for Epaper we using third party SDK So that this problem occur.I Removed SDK & the code is working fine now.
you should think UIBarButtonSystemItemFixedSpace effect.
eg:
UIBarButtonItem * space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
--
Why use a system navigation bar ?Custom navigation bar is nice!like:https://github.com/chenliangloveyou/EasyNavigation

How to reuse this ViewController code in an object oriented way?

I have an app with four viewControllers, and there is a lot of code that is repeating exactly the same way in the four view controllers, and I would like to know what is the best OOP way to write this code only one time and reuse it in the others view controllers.
This is the code from viewDidLoad of my first view controller:
- (void)viewDidLoad {
[super viewDidLoad];
[self setCanDisplayBannerAds:YES];
[[[self.tabBarController.viewControllers objectAtIndex:1] tabBarItem]setImage:[UIImage imageNamed:#"iconoBota30x30.png"]];
[[[self.tabBarController.viewControllers objectAtIndex:2] tabBarItem]setImage:[UIImage imageNamed:#"actividades.png"]];
[[[self.tabBarController.viewControllers objectAtIndex:3] tabBarItem]setImage:[UIImage imageNamed:#"nosotros.png"]];
float width = [UIScreen mainScreen].bounds.size.width;
float height = [UIScreen mainScreen].bounds.size.height;
static_iVar = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, width,height)];
static_iVar.delegate = self;
NSURL *url = [NSURL URLWithString:#"http://guiasdelsur.es"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[static_iVar loadRequest:requestObj];
[self.view addSubview:static_iVar];
//GESTURES
//DOWN
UISwipeGestureRecognizer *gest1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(hideTabBar:)];
gest1.delegate = self;
[gest1 setDirection:UISwipeGestureRecognizerDirectionDown];
[self.view addGestureRecognizer:gest1];
//UP
UISwipeGestureRecognizer *gest2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(showTabBar:)];
gest2.delegate = self;
[gest2 setDirection:UISwipeGestureRecognizerDirectionUp];
[self.view addGestureRecognizer:gest2];
//first tab
_tabInicio = [[UITabBarItem alloc] initWithTitle:#"Inicio" image:[UIImage imageNamed:#"d.png"] tag:0];
[[UITabBar appearance] setSelectedImageTintColor:[UIColor greenColor]];
[self.tabBarController setSelectedIndex:0];
self.tabBarItem = _tabInicio;
_cToolBar = [[UIToolbar alloc] init];
_cToolBar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
NSMutableArray *items = [[NSMutableArray alloc] init];
[_cToolBar setBarStyle:UIBarStyleBlack];
[_cToolBar setTranslucent:YES ];
[_cToolBar setTintColor:[UIColor greenColor]];
// Do any additional setup after loading the view, typically from a nib.
NSString *backArrowString = #"\U000025C0\U0000FE0E Atrás"; //BLACK LEFT-POINTING TRIANGLE PLUS VARIATION SELECTOR
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:backArrowString style:UIBarButtonItemStyleDone target:nil action:#selector(goBack)];
UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:nil action:#selector(refreshControl)];
[right setTintColor:[UIColor greenColor]];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:back];
[items addObject:flexibleSpace];
[items addObject:right];
[_cToolBar setItems:items animated:NO];
[self.view addSubview:_cToolBar];
//iAD
ADBannerView * adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.frame = CGRectOffset(adView.frame, 0, 44);
adView.delegate = self;
[self.view addSubview:adView];
//loading indicator
_indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[_indicator setCenter:self.view.center];
[_indicator setHidesWhenStopped:YES];
[self.view addSubview:_indicator];
[_indicator startAnimating];
}
And this is the code from viewDidLoad method of my secondViewController, very similar to the first one. The differences in all my view controllers are the same than those ones.
- (void)viewDidLoad {
[super viewDidLoad];
[self setCanDisplayBannerAds:YES];
float width = [UIScreen mainScreen].bounds.size.width;
float height = [UIScreen mainScreen].bounds.size.height;
static_iVar2 = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, width,height)];
static_iVar2.delegate = self;
NSURL *url = [NSURL URLWithString:#"http://guiasdelsur.es/category/programas/"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[static_iVar2 loadRequest:requestObj];
[self.view addSubview:static_iVar2];
//second tab
_secondTab = [[UITabBarItem alloc] initWithTitle:#"Programas" image:[UIImage imageNamed:#"iconoBota30x30.png"] tag:1];
[[UITabBar appearance] setSelectedImageTintColor:[UIColor greenColor]];
[self.tabBarController setSelectedIndex:1];
self.tabBarItem = _secondTab;
//GESTURES
//DOWN
UISwipeGestureRecognizer *gest1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(hideTabBar:)];
gest1.delegate = self;
[gest1 setDirection:UISwipeGestureRecognizerDirectionDown];
[self.view addGestureRecognizer:gest1];
//UP
UISwipeGestureRecognizer *gest2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(showTabBar:)];
gest2.delegate = self;
[gest2 setDirection:UISwipeGestureRecognizerDirectionUp];
[self.view addGestureRecognizer:gest2];
UIToolbar *cToolBar = [[UIToolbar alloc] init];
cToolBar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
NSMutableArray *items = [[NSMutableArray alloc] init];
[cToolBar setBarStyle:UIBarStyleBlack];
[cToolBar setTranslucent:YES ];
[cToolBar setTintColor:[UIColor greenColor]];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil ];
UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:nil action:#selector(refreshControl)];
[right setTintColor:[UIColor greenColor]];
[items addObject:flexibleSpace];
[items addObject:right];
[cToolBar setItems:items animated:NO];
[self.view addSubview:cToolBar];
//iAD
ADBannerView * adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.frame = CGRectOffset(adView.frame, 0, 44);
adView.delegate = self;
[self.view addSubview:adView];
//Indicator
_indicador = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[_indicador setCenter:self.view.center];
[_indicador setHidesWhenStopped:YES];
[self.view addSubview:_indicador];
[_indicador startAnimating];
}
It is a very good idea to refactor this code; it is an example of DRY. The best way in this case would be to use inheritance: write all common code in the ViewController class, e.g.
ViewController.h
#interface ViewController : UIViewController {
// Variables which are declared here can also be used in the subclasses
}
ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
[self setCanDisplayBannerAds:YES];
// more common code
...
}
and have your other view controllers inherit from ViewController (rather than UIViewController):
FirstViewController.h
#interface FirstViewController : ViewController
...
FirstViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// more specific code
[[[self.tabBarController.viewControllers objectAtIndex:1] tabBarItem]setImage:[UIImage imageNamed:#"iconoBota30x30.png"]];
[[[self.tabBarController.viewControllers objectAtIndex:2] tabBarItem]setImage:[UIImage imageNamed:#"actividades.png"]];
[[[self.tabBarController.viewControllers objectAtIndex:3] tabBarItem]setImage:[UIImage imageNamed:#"nosotros.png"]];
...
}

Using cameraOverlayView with UIImagePickerController

I have an app that uses the UIImagePickerController to take a picture. The problem is that I only want the camera option to be available and I understand that I need to hide the standard controls:
cameraUI.showsCameraControls=NO;
and use a cameraOverlayView to provide my own controls. I have had a look at Apple's PhotoPicker project already and my initial problem is how do I get an Overlay object onto my storyboard? I can't find such an object in the library.
Here is the code :
toolBar=[[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-54, self.view.frame.size.width, 55)];
toolBar.barStyle = UIBarStyleBlackOpaque;
NSArray *items=[NSArray arrayWithObjects:
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelPicture)],
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:#selector(shootPicture)],
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
nil];
[toolBar setItems:items];
// create the overlay view
overlayView = [[OverlayView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-44)];
// important - it needs to be transparent so the camera preview shows through!
overlayView.opaque=NO;
overlayView.backgroundColor=[UIColor clearColor];
// parent view for our overlay
UIView *cameraView=[[UIView alloc] initWithFrame:self.view.bounds];
[cameraView addSubview:overlayView];
[cameraView addSubview:toolBar];
imagePickerController = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO){
NSLog(#"Camera not available");
return;
}
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.delegate = self;
// hide the camera controls
imagePickerController.showsCameraControls=NO;
imagePickerController.wantsFullScreenLayout = YES;
[imagePickerController setCameraOverlayView:cameraView];
[self presentViewController:imagePickerController animated:YES completion:nil];
Declare this in your header file :
UIImagePickerController * imagePickerController;
UIToolbar *toolBar;
OverlayView *overlayView;
Add this OverlayView.h and .m Class from Apples PhotoPicker.
Actions for capturing photo using custom camera button:
-(void) shootPicture {
[imagePickerController takePicture];
}
- (IBAction)cancelPicture {
[self dismissViewControllerAnimated:YES completion:nil];
}
Output will come like this below attached screenshot (I have added a capture button and cancel button in custom overlay view):
Happy Coding :)
Here is my code. If you want the camera to appear as soon as the view controller opens, make sure you initialize the UIImagePickerController in viewDidAppear like I did (viewDidLoad does not work).
#interface CameraViewController ()
#property UIImagePickerController *PickerController;
#property CGFloat HeightOfButtons;
#end
- (UIView *)createCustomOverlayView
{
// Main overlay view created
UIView *main_overlay_view = [[UIView alloc] initWithFrame:self.view.bounds];
// Clear view (live camera feed) created and added to main overlay view
// ------------------------------------------------------------------------
UIView *clear_view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - self.HeightOfButtons)];
clear_view.opaque = NO;
clear_view.backgroundColor = [UIColor clearColor];
[main_overlay_view addSubview:clear_view];
// ------------------------------------------------------------------------
// Creates two red buttons on the bottom of the view (on top of the live camera feed)
// Then adds the buttons to the main overlay view
// You can, of course, customize these buttons however you want
// ------------------------------------------------------------------------
for(int i = 0; i < 2; i++) {
self.HeightOfButtons = 100;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
// when a button is touched, UIImagePickerController snaps a picture
[button addTarget:self action:#selector(testIfButtonResponds) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake( i * self.view.frame.size.width / 2, self.view.frame.size.height - self.HeightOfButtons, self.view.frame.size.width / 2, self.HeightOfButtons);
[button setBackgroundColor:[UIColor redColor]];
[main_overlay_view addSubview:button];
}
// ------------------------------------------------------------------------
return main_overlay_view;
}
- (void)makeCustomCameraAppear
{
self.PickerController = [[UIImagePickerController alloc] init];
self.PickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
self.PickerController.showsCameraControls = NO;
self.PickerController.delegate = self;
UIView *overlay_view = [self createCustomOverlayView];
[self.PickerController setCameraOverlayView:overlay_view];
[self presentViewController:self.PickerController animated:YES completion:NULL];
}
- (void)viewDidAppear:(BOOL)animated
{
[self makeCustomCameraAppear];
}
great information. To add a little to what Raghu did:
up top:
UIImagePicker *_imagePicker;
//inside your take a picture method
UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
toolBar.barStyle = UIBarStyleBlackOpaque;
NSArray *items=[NSArray arrayWithObjects:
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelPicker)],
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:#selector(shootPicture)],
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
nil];
[toolBar setItems:items];
toolBar.backgroundColor=[UIColor blackColor];
UIImageView *tp=[[UIImageView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-100, self.view.frame.size.width, 100)];
tp.userInteractionEnabled=YES;
tp.backgroundColor=[UIColor blackColor];
[tp addSubview:toolBar]; //add the toolbar to the uiimageview
_imagePicker=[[UIImagePickerController alloc]init];
_imagePicker.sourceType=UIImagePickerControllerSourceTypeCamera;
_imagePicker.delegate = self;
_imagePicker.showsCameraControls=NO; //we will make custom controls.
[_imagePicker.view addSubview:tp];
-(void)cancelPicker{
//get rid of the image picker
[self dismissViewControllerAnimated:YES completion:nil]; //dismiss uiimagepicker
_imagePicker=nil;
}
//take the picture, it will then go directly to - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info method
-(void)shootPicture{
[_imagePicker takePicture];
}

Landscape SubViews not filling self.view

I have a UIView whose subview does not want to completely fill self.view after rotating into landscape mode. I have tried many things from setting autoresize masks to setting the frames of subviews after rotation, they either don't work, or the problem becomes much worse. Here is an example with a UINavigationBar that experiences this problem (navBar does not completely fill landscape width). The code:
- (void)loadView {
self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];
self.view.backgroundColor = [UIColor blueColor];
myBar =[[UINavigationBar alloc] init];
myBar.frame = CGRectMake (0, 0, self.view.frame.size.width, 44);
UINavigationItem *navItem =[[[UINavigationItem alloc] initWithTitle:#"Title"] autorelease];
UIBarButtonItem *rightButton =[[[UIBarButtonItem alloc] initWithTitle: #"Email" style: UIBarButtonItemStylePlain target: self action:#selector (rightButtonPressed)] autorelease];
navItem.rightBarButtonItem = rightButton;
UIBarButtonItem *leftButton =[[[UIBarButtonItem alloc] initWithTitle: #"About" style: UIBarButtonItemStylePlain target: self action:#selector (leftButtonPressed)] autorelease];
navItem.leftBarButtonItem = leftButton;
myBar.barStyle = UIBarStyleDefault;
myBar.items =[NSArray arrayWithObject:navItem];
[self.view addSubview:myBar];
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
#end
Just add a single line of code at the end of your code:
- (void)loadView
{
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view.backgroundColor = [UIColor blueColor];
UINavigationBar *myBar;
myBar =[[UINavigationBar alloc] init];
myBar.frame = CGRectMake (0, 0, self.view.frame.size.width, 44);
UINavigationItem *navItem =[[UINavigationItem alloc] initWithTitle:#"Title"];
UIBarButtonItem *rightButton =[[UIBarButtonItem alloc] initWithTitle: #"Email" style: UIBarButtonItemStylePlain target: self action:#selector (rightButtonPressed)];
navItem.rightBarButtonItem = rightButton;
UIBarButtonItem *leftButton =[[UIBarButtonItem alloc] initWithTitle: #"About" style: UIBarButtonItemStylePlain target: self action:#selector (leftButtonPressed)];
navItem.leftBarButtonItem = leftButton;
myBar.barStyle = UIBarStyleDefault;
myBar.items =[NSArray arrayWithObject:navItem];
[self.view addSubview:myBar];
[myBar setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
}
I removed autorelease because I used ARC. You can add autorelease again. Have a good day friend.

UIToolbar creation

i have created a toolbar as below shown code and now i dont knw how to add the items to it.
- (void)viewDidLoad
{
[super viewDidLoad];
UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 418, 350, 44);
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:[[[UIBarButtonItem alloc] init] autorelease]];
[toolbar setItems:items animated:YES];
[items release];
[self.view addSubview:toolbar];
[toolbar release];
}
Try this code
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithTitle:#"Item" style:UIBarButtonItemStyleBordered target:self action:#selector(action:)];
NSArray *items = [NSArray arrayWithObjects: customItem, nil];
[toolbar setItems:items animated:NO];
[customItem release];
You can also refer to this link:
How to create this toolbar programmatically
Please do google search and Stack Overflow search before posting.
use
[self setToolbarItems: buttons animated:NO];
as u want to set toolbaritems and uiviewcontroller has toolbar itself.

Resources