我看了很多文章来帮助我,但找不到答案......
我想在键盘处于活动状态时用“完成”按钮替换我的右栏按钮,然后在编辑完成后将其替换回来......
我已经有一个右栏按钮,它是一个分段控件,并在视图加载时设置。我不确定创建后是否可以切换栏按钮?
以下是一些屏幕截图...
想在键盘出现时将上一个/下一个按钮更改为完成按钮
以下是显示键盘时触发的代码...代码执行但未显示按钮...
- (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;