How to programmatically add a UINavigationBar and a back button on it

I'm a newbie creating an application similar to the Notes app for iPhone using a UITextView . I get a textView and a string, and it works fine.

My problem is that I want to add a UINavigationBar button and vice versa. And I want to add a UIToolBar at the bottom and 2 toolBarItems on it, how to do it programmatically. Any help would be a big push for me.

below is a code snippet.

NoteView.h

 @interface NoteView : UITextView <UITextViewDelegate,UITabBarControllerDelegate> { } 

NoteView.m

 - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor colorWithRed:0.6f green:0.6f blue:1.0f alpha:1.0f]; self.font = [UIFont fontWithName:@"MarkerFelt-Thin" size:20]; self.contentMode = UIViewContentModeRedraw; } return self; } - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.2f].CGColor); CGContextSetLineWidth(context, 1.0f); CGContextBeginPath(context); NSUInteger numberOfLines = (self.contentSize.height + self.bounds.size.height) / self.font.leading; CGFloat baselineOffset = 6.0f; for (int x = 0; x < numberOfLines; x++) { CGContextMoveToPoint(context, self.bounds.origin.x, self.font.leading*x + 0.5f + baselineOffset); CGContextAddLineToPoint(context, self.bounds.size.width, self.font.leading*x + 0.5f + baselineOffset); } CGContextClosePath(context); CGContextStrokePath(context); } 

AddNotesViewController.h

 @interface AddNotesViewController : UIViewController <UITextViewDelegate,UITabBarDelegate> { NoteView *note; } @property (nonatomic, retain) NoteView *note; @end 

AddNotesViewController.m

 - (void)loadView { [super loadView]; self.note = [[[NoteView alloc] initWithFrame:self.view.bounds] autorelease]; [self.view addSubview:note]; note.delegate = self; note.text=@ ""; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { [note setNeedsDisplay]; } - (void)textViewDidBeginEditing:(UITextView *)textView { CGRect frame = self.view.bounds; frame.size.height -= KEYBOARD_HEIGHT; note.frame = frame; } - (void)textViewDidEndEditing:(UITextView *)textView { note.frame = self.view.bounds; } - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { if ([text isEqualToString:@"\n"]) { [textView resignFirstResponder]; return NO; } return YES; } 

Please tell me how and where to add the navigation bar, return button and toolbar, 2 tool elements on it. Thanks in advance...

+3
source share
2 answers

Image of navigation bar

 UINavigationBar *navBar = [[self navigationController] navigationBar]; UIImage *image = [UIImage imageNamed:@"TopBar.png"]; [navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault]; 

Back button

 -(void)getBackBtn { UIButton *Btn =[UIButton buttonWithType:UIButtonTypeCustom]; [Btn setFrame:CGRectMake(0.0f,0.0f,50.0f,30.0f)]; [Btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"back.png"]] forState:UIControlStateNormal]; //[Btn setTitle:@"OK" forState:UIControlStateNormal]; //Btn.titleLabel.font = [UIFont fontWithName:@"Georgia" size:14]; [Btn addTarget:self action:@selector(backBtnPress:) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithCustomView:Btn]; [self.navigationItem setLeftBarButtonItem:addButton]; } 

BackButtonAction

 -(IBAction)backBtnPress:(id)sender { } 

View in NavigationBar

To view in navigationBar you can follow my answer Link

+3
source

use this code ....

 UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Back", @"") style:UIBarButtonItemStyleDone target:self action:@selector(YourActionMethod:)]; self.navigationItem.leftBarButtonItem = addButton; 
0
source

Source: https://habr.com/ru/post/1492341/


All Articles