1

我看了很多文章来帮助我,但找不到答案......

我想在键盘处于活动状态时用“完成”按钮替换我的右栏按钮,然后在编辑完成后将其替换回来......

我已经有一个右栏按钮,它是一个分段控件,并在视图加载时设置。我不确定创建后是否可以切换栏按钮?

以下是一些屏幕截图...

想在键盘出现时将上一个/下一个按钮更改为完成按钮

想在键盘出现时将上一个/下一个按钮更改为完成按钮

带键盘的屏幕

以下是显示键盘时触发的代码...代码执行但未显示按钮...

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
// Replace the Right Navigation Button with done button

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                                            target:self
                                                                            action:@selector(doneEditing)];
self.navigationItem.rightBarButtonItem = doneButton;

// Slide up the view

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];

CGRect rect = self.view.frame;

// Move the view .... Note need to calculate this based on the size of the keyboard !!

rect.origin.y -= 120.0f;
rect.size.height += 120.f;

self.view.frame = rect;

// Resize the text view .... Note need to calculate this based on screen size !!

CGSize frameSize = self.reviewTextView.frame.size;
CGPoint framePos = self.reviewTextView.frame.origin;
[self.reviewTextView setFrame:CGRectMake(framePos.x, framePos.y, frameSize.width, 200.0f)];

[UIView commitAnimations];

return YES;
}

以下是视图加载时设置上一个/下一个按钮的代码段

    //set up the segmented control and add it to the nav bar rightBartButtonItem

UISegmentedControl *segmentControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"\U000025C0\U0000FE0E Prev", @"Next \U000025B6\U0000FE0E", nil]];
UIBarButtonItem * segmentControlButton = [[UIBarButtonItem alloc] initWithCustomView:segmentControl];
[segmentControl setBackgroundColor:[UIColor clearColor]];
segmentControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentControl.frame = CGRectMake(0, 0, 120, 30);
[segmentControl setMomentary:YES];
[segmentControl addTarget:self
                   action:@selector(onSegmentChanged:)
         forControlEvents:UIControlEventValueChanged];
self.navigationItem.rightBarButtonItem = segmentControlButton;
4

3 回答 3

1

有时您需要提示系统重新绘制带有更改的栏,请尝试调用[self.navigationController.navigationBar setNeedsDisplay].

于 2013-09-23T11:02:11.573 回答
0

对不起,这是我的编码错误...

来自用户 MICANTOX 的支持您引发了一个想法,即我有正确的代码但在错误的地方!由于这是一个子视图,我没有在正确的位置执行代码....代码现在已放置在父视图中

于 2013-09-23T15:14:20.527 回答
0

试试这个,

if(keyboardIsActive){
UIBarButtonItem *menuButton = [[UIBarButtonItem alloc]
                                   initWithTitle:@"Done"
                                   style:UIBarButtonItemStyleBordered
                                   target:self.revealViewController
                                   action:@selector(revealToggle:)];

    destinationViewController.navigationItem.leftBarButtonItem = menuButton;}
于 2013-09-23T10:51:39.050 回答