저는 이것을 위해 javascript를 사용하고 있습니다. 코드를 좀 더 체계적으로 유지하기 위해 클래스를 사용하고 있습니다 (코드에서 this
을 볼 수 있습니다).하지만 그럴 필요는 없습니다. 오브젝티브 -C에서
// this is used to get the current coordinates of the selection - not very efficient, so it shouldn't be called too often
this.updateOffset = function() {
try{
var sel = window.getSelection();
range = sel.getRangeAt(0);
if(this.tmpSpan==null){
this.tmpSpan = document.createElement('span');
}
range.insertNode(this.tmpSpan);
this.yOffset = this.tmpSpan.offsetTop;
this.xOffset = this.tmpSpan.offsetLeft;
this.tmpSpan.parentNode.removeChild(this.tmpSpan);
}
catch(exc){
log('updateOffset:' + exc.toString());
}
}
// eContent is the div with 'contenteditable', while visibleHeight is an int, set from objective-c (depending on where the webview is positioned, keyboard height and screen height)
this.scrollToVisible = function(){
try {
if(this.eContent.clientHeight>this.visibleHeight){
this.updateOffset();
if(this.yOffset<window.pageYOffset){
window.scrollTo(0, this.yOffset);
}
else if(this.yOffset-window.pageYOffset>this.visibleHeight){
window.scrollTo(0, this.yOffset-this.visibleHeight);
}
}
}
catch (exc){
log('scrollToVisible: ', exc.toString());
}
}
나는 보여주는 키보드 동안 visibleHeight을 설정하고 키보드가 보여주는 완료되면 이후 scrollToVisible
를 호출하고 있습니다.
-(void)setVisibleHeight:(int)height{
[self stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"docState.visibleHeight=%d", height]];
}
-(void)scrollToVisible{
[self stringByEvaluatingJavaScriptFromString:@"docState.scrollToVisible()"];
}
scrollToVisible
는 자바 스크립트 이벤트라고 : onKeyUp에, onpaset, oncut 여러 라인 '복귀'또는 포장을 누를 때 문제를 해결한다.
이 방법을 선택하는 경우 자바 스크립트를 스크롤 할 때 매우주의해야합니다. 그렇지 않으면 UIWebview 컨트롤에 몇 가지 문제가 발생할 수 있습니다 (예 : 잘못된 위치에 커서를 놓고 커서를 자동으로 이동). 문서 등의 위)
편집
visibleHeight
에 대한 일부 설명합니다. 내가 기억할 수있는 것에서는 자바 스크립트에서 실제로 보이는 높이를 얻을 수 없었기 때문에 이것을 사용했습니다 (document.body.clientHeight
에는 키보드 뒤의 영역도 포함됩니다). 내가 UIWebView
의 서브 클래스에서이 전화 드렸습니다
- (void)keyboardWillShow:(NSNotification *)notification {
...
NSDictionary *userInfo = [notification userInfo];
NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
CGRect kbRect = [self.window convertRect:keyboardRect fromView:nil];
_kbRect = kbRect;
CGPoint sorigin = [self.superview convertPoint:self.frame.origin toView:nil];
int visibleHeight = _kbRect.origin.y-sorigin.y-_tlbInputAccessory.frame.size.height-lkPadBottom; // _tlbInputAccessory is a custom accessory view
[self stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"docState.setVisibleHeight(%d)", height]];
...
}
주, 그래서 self
가를 나타냅니다 : 나는 전체 화면으로 UIWebView
을 제시하고 있습니다 때문에 다음과 같이
, 나는 눈에 보이는 높이를 설정하고있어 UIWebView
컨트롤.
호출 방법 - (void) setVisibleHeight : (int) height 나는 높이를 의미합니까? –
@MoorthyTheBoss는 'visibleHeight'에 대한 자세한 정보는 편집을 참조하십시오. –
lkPadBottom이라는 오브젝트가 무엇인지 알려 주실 수 있습니까? –