3

我是新手,试图使用UITextView. 我得到了textView和线,它工作正常。

我的问题是我想在UINavigationBar上面添加一个和返回按钮。我想UIToolBar在底部添加一个并在其上添加 2 个 toolBarItems 如何以编程方式执行此操作。任何帮助对我来说都是一个很大的推动力..

下面是代码片段。

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

添加NotesViewController.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;
}

请告诉我如何以及在哪里添加导航栏,后退按钮和工具栏,2 toolBarItems 就可以了。在此先感谢...

4

2 回答 2

3

导航栏图片

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

返回键

-(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
{
}  

在导航栏上查看

对于在导航栏上查看,您可以按照我的答案链接

于 2012-11-29T05:59:29.543 回答
0

使用此代码....

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


self.navigationItem.leftBarButtonItem = addButton;
于 2012-11-26T13:40:05.977 回答