[英]Adding UIWebView inside UIScrollView XAMARIN.IOS C#
I'd like to add a UIWebView (and some other elements) in a UIScrollView.我想在 UIScrollView 中添加一个 UIWebView(和一些其他元素)。
First of all, my UIWebView has not a fix text and could be change.首先,我的 UIWebView 没有修复文本,可以更改。
So, as I saw in examples and tutorials I can do it like this:所以,正如我在示例和教程中看到的,我可以这样做:
public class testArticleViewController : UIViewController
{
private UIScrollView _scrollView;
//Post elements
UIWebView _artcileTextWeb;
public testArticleViewController()
{
Title = "test";
View.BackgroundColor = UIColor.White;
}
public override void ViewDidLoad()
{
string text = @"L'un des deux meilleurs du monde, Lionel Messi, va-t-il manquer la Coupe du monde ? L'Argentine se retrouve dans une situation extrêmement délicate dans la course à la qualification au Mondial 2018 en faisant match nul (0-0) jeudi face au Pérou.<br />
<div>
<div>
<div>
<article>
<div>
<div>
<p ><br />Le risque de voir une Coupe du Monde en Russie sans Lionel Messi est bien réel : sixième et en dehors de la zone de qualification à une journée de la fin, l'Albiceleste doit impérativement s'imposer mardi face à l'Équateur, déjà éliminé.<br /><br />Cinq équipes se tiennent en deux points, entre le Chili (3e, 26 points) et le Paraguay (7e, 24 points) qui garde un mince espoir de qualification grâce à son succès de jeudi (2-1) face à la Colombie (4e, 26 points).<br /><br />L'Argentine ne pointe qu'en sixième position, avec le même nombre de points que les Péruviens (5e, 25), qui occupent pour le moment une place de barragiste et rêvent à une première participation au Mondial depuis 1982.</p>
<h3>Sampaoli est «très confiant»</h3>
<p>Même s'il admet que la position de l'Argentine <em>«n'est pas très confortable»</em>, le sélectionneur Jorge Sampaoli se veut <em>«très confiant dans le fait que nous allons être au Mondial»</em>. Pour le match crucial de jeudi face au Pérou, la Fédération argentine avait misé sur l'ambiance bouillante du mythique stade de la Bombonera.<br /><br />Poussés par quelque 50 000 supporters, les locaux ont eu beaucoup de mal à trouver des espaces face à des Péruviens bien regroupés derrière. Messi a cru ouvrir le score dès la 13e minute, sur une belle combinaison sur corner, mais un défenseur s'est jeté in extremis pour dévier un ballon qui semblait prendre la direction des filets. La Bombonera a soupiré une nouvelle fois dix minutes plus tard, quand Di Maria - remplacé à la pause - a manqué une belle occasion en tirant au-dessus.<br /><br />Les nerfs à vif, les supporters ont retenu leur souffle à la 33e, en voyant Farfan manquer le cadre de peu sur un centre à ras de terre Trauco. Messi bien touché le poteau au retour des vestiaires, mais l'Albiceleste a livré une nouvelle fois une copie bien pâle. <em>«On ne peut pas en demander plus à Leo Messi. Il a eu des opportunités, les a créées, a eu des balles de but. On a eu un Messi très intense, celui dont l'Argentine a besoin»</em>, a considéré Sampaoli.<br /><br />La dernière fois que l'Albiceleste a raté un Mondial (celui de 1970, au Mexique), elle avait été condamnée par un match nul (2-2) face au Pérou, dans ce même stade de la Bombonera. Cette fois, il reste encore un match, un seul, pour tout changer.</p>
</div>
</div>
</article>
</div>
</div>
</div>";
var padding = 10;
View.BackgroundColor = UIColor.White;
_scrollView = new UIScrollView()
{ ShowsHorizontalScrollIndicator = false, AutoresizingMask = UIViewAutoresizing.FlexibleHeight };
//Text article
_artcileTextWeb = new UIWebView();
_artcileTextWeb.LoadHtmlString(text, null);
_artcileTextWeb.ScrollView.ScrollEnabled = false;
var view1 = new UIView { BackgroundColor = UIColor.Blue };
var view3 = new UIView { BackgroundColor = UIColor.Green };
Add(_scrollView);
View.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
View.AddConstraints(
_scrollView.AtLeftOf(View),
_scrollView.AtTopOf(View),
_scrollView.WithSameWidth(View),
_scrollView.WithSameHeight(View));
_scrollView.Add(view1);
_scrollView.Add(_artcileTextWeb);
_scrollView.Add(view3);
_scrollView.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
var basicwidth = (UIScreen.MainScreen.Bounds.Width - 3 * padding) / 2;
var basicheight = basicwidth / 2;
_scrollView.AddConstraints(
view1.AtTopOf(_scrollView, UIApplication.SharedApplication.StatusBarFrame.Height),
view1.AtLeftOf(_scrollView, padding),
view1.Width().EqualTo(basicwidth),
view1.Height().EqualTo(basicheight),
view3.WithSameTop(view1),
view3.Left().EqualTo().RightOf(view1).Plus(padding),
view3.WithSameHeight(view1),
view3.WithSameWidth(view1),
_artcileTextWeb.Below(view1, padding),
_artcileTextWeb.WithSameLeft(view1),
_artcileTextWeb.WithSameRight(view3),
_artcileTextWeb.WithSameHeight(_scrollView)
);
}
}
If you run it, you will see that the UIScrollView has the same height of screen and it's not dynamic!如果你运行它,你会看到 UIScrollView 具有相同的屏幕高度并且它不是动态的!
So, maybe it seems that the UIWebView has not a correct height (has not a corrected frame size)!所以,也许 UIWebView 似乎没有正确的高度(没有正确的框架大小)! If it's the case, how can I predict the Height of UIWebView?
如果是这样,我如何预测 UIWebView 的高度?
Even if I use CGRect for creating adding UIWebView's frame to my UIScrollView, I need a Height for it!即使我使用 CGRect 创建将 UIWebView 的框架添加到我的 UIScrollView,我也需要一个高度!
Do you have any idea dear developers?亲爱的开发人员,您有什么想法吗?
Re: Autolayouts in UIScrollView using Cirrious.FluentLayouts.Touch回复: 使用 Crillous.FluentLayouts.Touch 在 UIScrollView 中自动布局
Set the contentSize
of UIWebView
, and then let it AtBottomOf Scrollview
.设置
UIWebView
的contentSize
,然后让它AtBottomOf Scrollview
。
_artcileTextWeb.Below(view1, padding),
_artcileTextWeb.WithSameLeft(view1),
_artcileTextWeb.WithSameRight(view3),
_artcileTextWeb.Height().EqualTo(_artcileTextWeb.ScrollView.ContentSize.Height),
_artcileTextWeb.AtBottomOf(_scrollView)
But it didn't work , ScrollView.ContentSize.Height
returns the incorrect result.但它不起作用,
ScrollView.ContentSize.Height
返回不正确的结果。
_artcileTextWeb = new UIWebView(UIScreen.MainScreen.Bounds);
_artcileTextWeb.LoadHtmlString(text, null);
_artcileTextWeb.ScrollView.ScrollEnabled = false;
string result = _artcileTextWeb.EvaluateJavascript("document.body.offsetHeight;");
int height = Convert.ToInt32(result);
_scrollView.AddConstraints(
//xxxx
_artcileTextWeb.Below(view1, padding),
_artcileTextWeb.WithSameLeft(view1),
_artcileTextWeb.WithSameRight(view3),
_artcileTextWeb.Height().EqualTo(height),
_artcileTextWeb.AtBottomOf(_scrollView)
);
The result always returns 667 (the screen height), so I move calculation to the method LoadingFinished
,set the content height after webview has finished loading.结果总是返回 667(屏幕高度),所以我将计算移动到方法
LoadingFinished
,在 webview 加载完成后设置内容高度。
New a subclass of UIWebViewDelegate新建一个 UIWebViewDelegate 的子类
class MyDelegate : UIWebViewDelegate
{
UIScrollView mainView;
public MyDelegate(UIScrollView view) {
mainView = view;
}
public override void LoadingFinished(UIWebView webView)
{
string result = webView.EvaluateJavascript("document.body.offsetHeight;");
mainView.AddConstraints(webView.Height().EqualTo(Convert.ToInt64(result)));
}
}
In ViewController在视图控制器中
_artcileTextWeb = new UIWebView(UIScreen.MainScreen.Bounds);
_artcileTextWeb.Delegate = new MyDelegate(_scrollView);
_artcileTextWeb.LoadHtmlString(text, null);
_artcileTextWeb.ScrollView.ScrollEnabled = false;
_artcileTextWeb.Below(view1, padding),
_artcileTextWeb.AtLeftOf(_scrollView), //modify this line
_artcileTextWeb.WithSameWidth(_scrollView), //modify this line
_artcileTextWeb.AtBottomOf(_scrollView)
At last, it works.最后,它起作用了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.