草庐IT

c# - 在 UIScrollView XAMARIN.IOS C# 中添加 UIWebView

coder 2024-01-22 原文

我想在 UIScrollView 中添加一个 UIWebView(和一些其他元素)。

首先,我的 UIWebView 没有固定文本,可以更改。

所以,正如我在示例和教程中看到的那样,我可以这样做:

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&nbsp;? L'Argentine se retrouve dans une situation extr&ecirc;mement d&eacute;licate dans la course &agrave; la qualification au Mondial 2018 en faisant match nul (0-0) jeudi face au P&eacute;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&eacute;el&nbsp;: sixi&egrave;me et en dehors de la zone de qualification &agrave; une journ&eacute;e de la fin, l'Albiceleste doit imp&eacute;rativement s'imposer mardi face &agrave; l'&Eacute;quateur, d&eacute;j&agrave; &eacute;limin&eacute;.<br /><br />Cinq &eacute;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&acirc;ce &agrave; son succ&egrave;s de jeudi (2-1) face &agrave; la Colombie (4e, 26 points).<br /><br />L'Argentine ne pointe qu'en sixi&egrave;me position, avec le m&ecirc;me nombre de points que les P&eacute;ruviens (5e, 25), qui occupent pour le moment une place de barragiste et r&ecirc;vent &agrave; une premi&egrave;re participation au Mondial depuis 1982.</p>
<h3>Sampaoli est &laquo;tr&egrave;s confiant&raquo;</h3>
<p>M&ecirc;me s'il admet que la position de l'Argentine <em>&laquo;n'est pas tr&egrave;s confortable&raquo;</em>, le s&eacute;lectionneur Jorge Sampaoli se veut <em>&laquo;tr&egrave;s confiant dans le fait que nous allons &ecirc;tre au Mondial&raquo;</em>. Pour le match crucial de jeudi face au P&eacute;rou, la F&eacute;d&eacute;ration argentine avait mis&eacute; sur l'ambiance bouillante du mythique stade de la Bombonera.<br /><br />Pouss&eacute;s par quelque 50 000 supporters, les locaux ont eu beaucoup de mal &agrave; trouver des espaces face &agrave; des P&eacute;ruviens bien regroup&eacute;s derri&egrave;re. Messi a cru ouvrir le score d&egrave;s la 13e minute, sur une belle combinaison sur corner, mais un d&eacute;fenseur s'est jet&eacute; in extremis pour d&eacute;vier un ballon qui semblait prendre la direction des filets. La Bombonera a soupir&eacute; une nouvelle fois dix minutes plus tard, quand Di Maria - remplac&eacute; &agrave; la pause - a manqu&eacute; une belle occasion en tirant au-dessus.<br /><br />Les nerfs &agrave; vif, les supporters ont retenu leur souffle &agrave; la 33e, en voyant Farfan manquer le cadre de peu sur un centre &agrave; ras de terre Trauco. Messi bien touch&eacute; le poteau au retour des vestiaires, mais l'Albiceleste a livr&eacute; une nouvelle fois une copie bien p&acirc;le. <em>&laquo;On ne peut pas en demander plus &agrave; Leo Messi. Il a eu des opportunit&eacute;s, les a cr&eacute;&eacute;es, a eu des balles de but. On a eu un Messi tr&egrave;s intense, celui dont l'Argentine a besoin&raquo;</em>, a consid&eacute;r&eacute; Sampaoli.<br /><br />La derni&egrave;re fois que l'Albiceleste a rat&eacute; un Mondial (celui de 1970, au Mexique), elle avait &eacute;t&eacute; condamn&eacute;e par un match nul (2-2) face au P&eacute;rou, dans ce m&ecirc;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)

            );
        }



    }

如果你运行它,你会看到 UIScrollView 具有相同的屏幕高度并且它不是动态的!

所以,也许 UIWebView 的高度似乎不正确(没有更正的框架大小)!如果是这样,我如何预测 UIWebView 的高度?

即使我使用 CGRect 创建将 UIWebView 的框架添加到我的 UIScrollView,我也需要一个高度!

亲爱的开发者,您有什么想法吗?

最佳答案

回复:Autolayouts in UIScrollView using Cirrious.FluentLayouts.Touch

正确的解决方案:

设置UIWebViewcontentSize,然后让它AtBottomOf Scrollview

第一轮

_artcileTextWeb.Below(view1, padding),
_artcileTextWeb.WithSameLeft(view1),
_artcileTextWeb.WithSameRight(view3),
_artcileTextWeb.Height().EqualTo(_artcileTextWeb.ScrollView.ContentSize.Height),
_artcileTextWeb.AtBottomOf(_scrollView)

但是它不起作用,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)

        );

结果总是返回667(屏幕高度),所以我将计算移至方法LoadingFinished,设置webview加载完成后的内容高度。

第三轮

新建一个 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)));
    }
}

在 View Controller 中

_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)

终于成功了。

关于c# - 在 UIScrollView XAMARIN.IOS C# 中添加 UIWebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47046146/

有关c# - 在 UIScrollView XAMARIN.IOS C# 中添加 UIWebView的更多相关文章

  1. ruby - 我需要将 Bundler 本身添加到 Gemfile 中吗? - 2

    当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/

  2. ruby - 将 Bootstrap Less 添加到 Sinatra - 2

    我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它

  3. ruby - 续集在添加关联时访问many_to_many连接表 - 2

    我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以

  4. c# - 如何在 ruby​​ 中调用 C# dll? - 2

    如何在ruby​​中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL

  5. C# 到 Ruby sha1 base64 编码 - 2

    我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha

  6. ruby - 可以通过多少种方法将方法添加到 ruby​​ 对象? - 2

    当谈到运行时自省(introspection)和动态代码生成时,我认为ruby​​没有任何竞争对手,可能除了一些lisp方言。前几天,我正在做一些代码练习来探索ruby​​的动态功能,我开始想知道如何向现有对象添加方法。以下是我能想到的3种方法:obj=Object.new#addamethoddirectlydefobj.new_method...end#addamethodindirectlywiththesingletonclassclass这只是冰山一角,因为我还没有探索instance_eval、module_eval和define_method的各种组合。是否有在线/离线资

  7. ruby - 如何在 Ruby 中向现有方法定义添加语句 - 2

    我注意到类定义,如果我打开classMyClass,并在不覆盖的情况下添加一些东西我仍然得到了之前定义的原始方法。添加的新语句扩充了现有语句。但是对于方法定义,我仍然想要与类定义相同的行为,但是当我打开defmy_method时似乎,def中的现有语句和end被覆盖了,我需要重写一遍。那么有什么方法可以使方法定义的行为与定义相同,类似于super,但不一定是子类? 最佳答案 我想您正在寻找alias_method:classAalias_method:old_func,:funcdeffuncold_func#similartoca

  8. ruby-on-rails - 添加回形针新样式不影响旧上传的图像 - 2

    我有带有Logo图像的公司模型has_attached_file:logo我用他们的Logo创建了许多公司。现在,我需要添加新样式has_attached_file:logo,:styles=>{:small=>"30x15>",:medium=>"155x85>"}我是否应该重新上传所有旧数据以重新生成新样式?我不这么认为……或者有什么rake任务可以重新生成样式吗? 最佳答案 参见Thumbnail-Generation.如果rake任务不适合你,你应该能够在控制台中使用一个片段来调用重新处理!关于相关公司

  9. ruby - 我如何添加二进制数据来遏制 POST - 2

    我正在尝试使用Curbgem执行以下POST以解析云curl-XPOST\-H"X-Parse-Application-Id:PARSE_APP_ID"\-H"X-Parse-REST-API-Key:PARSE_API_KEY"\-H"Content-Type:image/jpeg"\--data-binary'@myPicture.jpg'\https://api.parse.com/1/files/pic.jpg用这个:curl=Curl::Easy.new("https://api.parse.com/1/files/lion.jpg")curl.multipart_form_

  10. 基于C#实现简易绘图工具【100010177】 - 2

    C#实现简易绘图工具一.引言实验目的:通过制作窗体应用程序(C#画图软件),熟悉基本的窗体设计过程以及控件设计,事件处理等,熟悉使用C#的winform窗体进行绘图的基本步骤,对于面向对象编程有更加深刻的体会.Tutorial任务设计一个具有基本功能的画图软件**·包括简单的新建文件,保存,重新绘图等功能**·实现一些基本图形的绘制,包括铅笔和基本形状等,学习橡皮工具的创建**·设计一个合理舒适的UI界面**注明:你可能需要先了解一些关于winform窗体应用程序绘图的基本知识,以及关于GDI+类和结构的知识二.实验环境Windows系统下的visualstudio2017C#窗体应用程序三.

随机推荐