1

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

是否有捷径可寻?

4

2 回答 2

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];

                }
            }
        }
    }
}

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

于 2012-05-18T19:15:12.400 回答
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 中获取了原始的上一个/下一个按钮,并在其末尾添加了一个完成按钮,但是您可以添加任何您想要的按钮。

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

希望这很有用!

于 2012-06-26T22:03:37.300 回答