![](/img/trans.png)
[英]WKWebview & UIImageView Inside UIScrollView programmatically autolayout Objective-C
[英]Objective-C: Set WKWebView height to fit content programmatically
我的應用程序很舊,所以很難找到很多關於 Objective-C 的文檔。
我正在將已棄用的 UIWebView 遷移到 WKWebView 並且我在根據內容以編程方式設置其高度時遇到問題,事實上我不知道該怎么做。
這是 header class,其中 webView 的高度被定義並綁定到 WKWebView 的高度約束,TMAnswerView.h:
#import "TMCustomView.h"
#import "TMAnswerModel.h"
#import "TMMainTestViewController.h"
#import <WebKit/WebKit.h>
@protocol TMAnswerViewProtocol <NSObject>
-(void) onCheckChanged:(TMAnswerModel*) answer;
@end
@interface TMAnswerView : TMCustomView
@property (nonatomic, strong) TMAnswerModel *answer;
@property (weak, nonatomic) IBOutlet UIButton *checkButton;
@property (weak, nonatomic) IBOutlet WKWebView *answerWebView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightOfWebView;
@property (weak, nonatomic) id<TMAnswerViewProtocol> delegate;
@property (nonatomic) TMMainTestViewConstrollerType viewControllerType;
-(void) setWebViewHeight;
@end
TMAnswerView.m:
#import "TMAnswerView.h"
#import "TMConsts.h"
#import "TMPersistanceManager.h"
#import <WebKit/WebKit.h>
@interface TMAnswerView () <WKNavigationDelegate>
//UIWebViewDelegate
@end
@implementation TMAnswerView
-(void)customInit{
}
-(void)setAnswer:(TMAnswerModel *)answer{
_answer = answer;
_answerWebView.navigationDelegate = self;
float font = 43; // 17;
NSNumber *type = [TMPersistanceManager fetchObjectForKey:PERSettingsFontSize];
if([type isEqual:SettingsFontSizeType1]){
font = font * 0.75;
}else if([type isEqual:SettingsFontSizeType3]){
font = font * 1.25;
}else if([type isEqual:SettingsFontSizeType4]){
font = font * 1.5;
}else if([type isEqual:SettingsFontSizeType5]){
font = font * 2;
}
NSString *htmlBody = [TMUtils getHTMLStringForMath:[answer.answer stringByReplacingOccurrencesOfString:@"$$" withString:@"$"] andFontSize:(int)font];
[_answerWebView loadHTMLString:htmlBody baseURL:[NSURL fileURLWithPath: [NSString stringWithFormat:@"%@/", [[NSBundle mainBundle] bundlePath]]]];
_answerWebView.scrollView.contentInset = UIEdgeInsetsMake(0,-8,0,-8);
}
#pragma mark - click listeners
- (IBAction)onCheckButton:(id)sender {
if(_viewControllerType != TMMainTestViewConstrollerTypeDoTest){
return;
}
_checkButton.selected = !_checkButton.selected;
if(_delegate){
[_delegate onCheckChanged:_answer];
}
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
[self setWebViewHeight];
}
//-(void)webViewDidFinishLoad:(WKWebView *)webView{
// [self setWebViewHeight];
//}
-(void) setWebViewHeight{
CGSize fittingSize = [_answerWebView sizeThatFits:CGSizeZero];
_heightOfWebView.constant = fittingSize.height;
}
@end
上面你會看到“setWebViewHeight”,我在這里設置了 UIWebView 高度,但現在我不知道如何將它適配到 WKWebView。
我認為為此目的代碼應該是相同的,但事實是 WebView 沒有根據內容調整高度並且顯示垂直裁剪(html 內容的第二行被裁剪)。
在下一個屏幕截圖中,您會看到最后兩個答案 web 視圖(應該分兩行顯示(因為它們不適合一行)被裁剪了。
編輯 1:
如果我手動設置高度然后它工作:
_heightOfWebView.constant = 42; // fittingSize.height;
好的,我會自己回答。
-(void) setWebViewHeight{
CGSize fittingSize = [_answerWebView sizeThatFits:CGSizeZero];
int height = _answerWebView.scrollView.contentSize.height;
_heightOfWebView.constant = height;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.