我正在从服务器下载文件并显示在我的ios设备中。文件类型可以是图像,pdf等任何类型,并且都可以正常加载。现在的问题是,文件或图像的某些部分隐藏在页面标题的后面。我想在文件加载后在文件顶部添加边距。
我为显示文件编写的代码:
private void getProducts() {
//stock variables
List<Integer> stockValue = new ArrayList<>();
stockValue.add(0);
int stockCriteria = 0;
int stockType = 1;
//price variables
List<Integer> priceValue = new ArrayList<>();
priceValue.add(0);
int priceCriteria = 0;
int priceType = 1;
//product name
List<String> productValue = new ArrayList<>();
int productCriteria = 0;
int productType = 5;
productValue.add("floral");
//setting the values of the json send as a body to the server
Filters filters = new Filters();
filters.setPrice(new Price(priceCriteria, priceType, priceValue));
filters.setStockQuantity(new StockQuantity(stockCriteria, stockType, stockValue));
//removed product name for test
//filters.setProductName(new ProductName(productCriteria, productType, productValue));
ProductAPIBodyRequest productAPIBodyRequest = new ProductAPIBodyRequest();
productAPIBodyRequest.setFilters(filters);
ProductAPIBodyRequest[] productAPIBodyRequests = {productAPIBodyRequest};
Call<JsonObject> products = marketApiCalls.getProducts(take, page, productAPIBodyRequests);
products.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
if (!response.isSuccessful()) {
return;
}
JsonObject body = response.body();
FullProductModel fullProductModel = gson.fromJson(body, FullProductModel.class);
for (int i = 0; i < fullProductModel.getData().size(); i++) {
com.twoverte.models.product.full_product.ResultData resultData = fullProductModel.getData().get(i).getResultData();
MiniProductModel model = new MiniProductModel(
resultData.getId(),
resultData.getImageUrl(),
resultData.getProductName(),
resultData.getShortDescription(),
resultData.getFullDescription(),
resultData.getVendorName(),
resultData.getVendorUrl(),
resultData.getUrl(),
resultData.getPictureList(),
resultData.getSku(),
resultData.getVendorImageUrl(),
resultData.getStockQuantity(),
resultData.getFavoritesCount(),
resultData.getPrice()
);
miniProductModelList.add(model);
}
productsAdapter.notifyDataSetChanged();
Log.d("allProducts", miniProductModelList.toString());
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
t.getMessage();
}
});
}
我已经在Class中创建了webView属性,
func openFile(file:String) {
let myBlog = file
let url = NSURL(string: myBlog)
let request = NSURLRequest(url: url! as URL)
webView.load(request as URLRequest)
self.view.addSubview(webView)
let pdfVC = UIViewController()
pdfVC.view.addSubview(webView)
pdfVC.title = "File"
self.navigationController?.pushViewController(pdfVC, animated: true)
self.navigationController!.navigationBar.tintColor = colors.whiteColor
}
在viewDidLoad()中,我写了:
var webView : WKWebView!
我得到的结果:
图像的某些部分位于标题的后面。我尝试了链接add margins in swift中给出的各种解决方案,但没有任何效果。
答案 0 :(得分:1)
我的建议是在需要显示它的控制器中创建WKWebView
的实例,在本例中为pdfVC
可以有模型,例如:
enum FileType {
case image(fileURL: URL)
case pdf(fileURL: URL)
}
创建FileViewController
的情况,并为其分配文件类型
func openFile(file:String) {
let pdfViewController = FileViewController()
pdfVC.fileType = FileType.pdf(fileURL: URL(string: "https://www.google.com")!) // example
pdfVC.title = "File"
navigationController?.pushViewController(pdfVC, animated: true)
}
在您需要显示文件的
VC
内:
通过这种方式,即使您拥有navigationBar
,您也不会遇到navigationBar
和topMargin
class FileViewController: UIViewController {
var fileType: FileType!
override func viewDidLoad() {
super.viewDidLoad()
var urlRequest: URLRequest!
switch fileType {
case .image(let url), .pdf(let url):
urlRequest = URLRequest(url: url)
@unknown default:
break
}
let webView = WKWebView(frame: view.frame)
webView.load(urlRequest)
self.view = webView
}
}
希望这对您有帮助!
答案 1 :(得分:0)
使用边界代替框架
webView = WKWebView(frame: self.view.Bounds)