3

I am newbie trying to make an app similar to Notes app of iPhone using UITextView. I am getting the textView and lines and it is working fine.

My problem is that I want to add a UINavigationBar and back button on it. And I want to add a UIToolBar at the bottom and 2 toolBarItems on it how to do this programmetically. Any help will be a great push up for me..

below is the 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 navigation bar , back button and tool bar ,2 toolBarItems on it.Thanks in advance...

Björn Kaiser
  • 9,882
  • 4
  • 37
  • 57
suvarna
  • 697
  • 2
  • 7
  • 10
  • Duplicate. [ButtonOnNav](http://stackoverflow.com/questions/2848055/add-button-to-navigationbar-programatically) & [NavBackButton](http://stackoverflow.com/questions/1441699/uinavigationcontroller-back-button-custom-text) are already answered. Go through [link](http://stackoverflow.com/faq#dontask) and [F&Q](http://stackoverflow.com/faq#questions). – HDdeveloper Nov 26 '12 at 13:52
  • http://stackoverflow.com/questions/13488710/how-to-set-a-picture-programmatically-in-a-navbar/13488781#13488781 – Rajneesh071 Nov 26 '12 at 14:47
  • are you using navigation controller or not – Rajneesh071 Nov 26 '12 at 14:48
  • yes , am using navigation controller – suvarna Nov 26 '12 at 14:58

2 Answers2

3

Navigation Bar Image

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 on NavigationBar

For View on navigationBar you can follow my answer Link

Community
  • 1
  • 1
Rajneesh071
  • 30,846
  • 15
  • 61
  • 74
0

use this code....

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


self.navigationItem.leftBarButtonItem = addButton;
Venk
  • 5,949
  • 9
  • 41
  • 52
  • I have to add that in AddNotesViewController.m of the - (void)loadView right?? – suvarna Nov 26 '12 at 13:46
  • In the above snippet ,You observed that drawRect in noteView.M r8? That code is to draw the lines in textView , so this barButtonItems is not displaying... – suvarna Nov 27 '12 at 05:36