1

UIWebViewのキーボード アクセサリ ビュー ツールバー (「戻る | 次へ」および「完了」ボタンが既にあるツールバー)にカメラ ボタンを追加する必要があります。

これを行う簡単な方法はありますか?

4

2 に答える 2

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 に答える
0

私はまだそれを行うためのより良い方法を探していますが、現時点での1つの解決策は、バーを破壊して次のように再作成することです:

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

                }
            }
        }
    }
}

ただし、previous、next、done 関数を再度実装する必要があります。しかし、それは最も難しい部分ではありません。

于 2012-05-18T19:15:12.400 に答える