将自定义UIButton添加到UIWebView的UIKeyboard附件视图中

时间:2012-05-18 18:59:03

标签: cocoa-touch uikeyboard

我需要将相机按钮添加到UIWebView的键盘配件视图工具栏(已经有“后退|下一步”和“完成”按钮的工具栏)。

有一种简单的方法吗?

2 个答案:

答案 0 :(得分:0)

我仍在寻找一种更好的方法来做到这一点,但目前的一个解决方案是破坏标准并重新创建它:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIWebView *web = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    NSURL *aURL = [NSURL URLWithString:@"http://www.google.com"];
    NSURLRequest *aRequest = [NSURLRequest requestWithURL:aURL];
    [web loadRequest:aRequest];
    [self.view addSubview:web];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}

- (void)keyboardWillShow:(NSNotification *)notification {
    [self performSelector:@selector(removeBar) withObject:nil afterDelay:0];
}

- (void)removeBar {
    UIWindow *keyboardWindow = nil;
    for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) {
        if (![[testWindow class] isEqual:[UIWindow class]]) {
            keyboardWindow = testWindow;
            break;
        }
    }

    for (UIView *possibleFormView in [keyboardWindow subviews]) {
        // iOS 5 sticks the UIWebFormView inside a UIPeripheralHostView.
        if ([[possibleFormView description] rangeOfString:@"UIPeripheralHostView"].location != NSNotFound) {
            for (UIView *subviewWhichIsPossibleFormView in [possibleFormView subviews]) {
                if ([[subviewWhichIsPossibleFormView description] rangeOfString:@"UIWebFormAccessory"].location != NSNotFound) {

                    UISegmentedControl *nav = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Previous",@"Next", nil]];
                    nav.momentary = YES; 
                    nav.frame = CGRectMake(5, 8, 120, 30); 
                    nav.segmentedControlStyle = UISegmentedControlStyleBar;
                    nav.tintColor = [UIColor colorWithWhite:0.25 alpha:1];
                    [nav addTarget:self action:@selector(navigate) forControlEvents:UIControlEventValueChanged];

                    UIBarButtonItem *pictureBtn =[[UIBarButtonItem alloc] initWithTitle:@"Picture" style:UIBarButtonItemStyleBordered target:self action:@selector(takePic:)];

                    UIBarButtonItem *flexButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
                    UIBarButtonItem *doneButton =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(resignKeyboard)];

                    NSArray *buttons = [NSArray arrayWithObjects:flexButton,pictureBtn,flexButton,doneButton, nil];
                    [(UIToolbar *)subviewWhichIsPossibleFormView addSubview:nav];
                    [(UIToolbar *)subviewWhichIsPossibleFormView setItems:buttons];

                }
            }
        }
    }
}

但是,你必须再次实现上一个,下一个和完成的功能。但这不是最困难的部分。

答案 1 :(得分:0)

有一种更简单的方法可以做到这一点。

将键盘通知保留在viewDidLoad

之后,您只需要以下方法:

-(void)keyboardDidShow:(NSNotification*)notif
{
    NSArray *array = [[UIApplication sharedApplication] windows];

    for (UIWindow* wind in array) {
        for (UIView* currView in wind.subviews) {
            if ([[currView description] hasPrefix:@"<UIPeripheralHostView"]) {
                for (UIView* perView in currView.subviews) {
                    if ([[perView description] hasPrefix:@"<UIWebFormAccessory"]) {

                        UIBarButtonItem *doneBttn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(resignKeyBoard)];

                        NSMutableArray *arr = [[NSMutableArray alloc] initWithArray: [(UIToolbar *) perView items]];

                    [arr addObject:doneBttn];

                    [(UIToolbar *) perView setItems:arr];
                    }
                }

            }
        }
    }
}

我基本上从UIWebFormAccessory中取出了原来的prev / next按钮,并在其末尾添加了一个完成按钮,但是你可以添加你想要的按钮。

有些人可能认为这不应该在-(void)keyboardDidShow:中实现,因为您在屏幕上显示后正在更改UI,但到目前为止我还没有发现任何问题。 (仅在模拟器中测试)

希望这很有用!