代码之家  ›  专栏  ›  技术社区  ›  user3713929

如何向Web视图应用程序添加上边距?

  •  0
  • user3713929  · 技术社区  · 8 年前

    我有一个Web视图应用程序,它有一个顶部菜单栏。在iphone x之前一切正常。 如您所见,它有一个顶部菜单栏,顶部需要一些空间来清除iphone x的扬声器。有什么方法可以使web视图更小吗?我试过限制条件,但没能成功。

    我可以在顶部缩小WebView并添加一些背景色/渐变色或其他内容吗?正如你在图片中看到的,当我向下滚动时,我可以看到扬声器两侧和菜单栏上方的内容(应该在网络视图的最顶部)。

    Iphone Example

    这是我的类定义的开始:

        public partial class WebViewController : UIViewController
    {
    
        LoadingOverlay loadPop;
        bool loadPopStatus = false;
    
        public override bool PrefersStatusBarHidden(){
            return true;
        }
    
        static bool UserInterfaceIdiomIsPhone
        {
            get { return UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone; }
        }
    
        protected WebViewController(IntPtr handle) : base(handle)
        {
            // Note: this .ctor should not contain any initialization logic.
    
        }
    
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            NSUserDefaults.StandardUserDefaults.Synchronize();
            var username = NSUserDefaults.StandardUserDefaults.StringForKey("username");
            var login_token = NSUserDefaults.StandardUserDefaults.StringForKey("login_token");
            var refresh_token = NSUserDefaults.StandardUserDefaults.StringForKey("refresh_token");
            var company_name = NSUserDefaults.StandardUserDefaults.StringForKey("company");
    
            // Intercept URL loading to handle native calls from browser
            WebView.ShouldStartLoad += HandleShouldStartLoad;
            //Change status bar background
            //WebView.ScrollView.ContentInset = new UIEdgeInsets(20, 20, 20, 20);
            //WebView.ScrollView.BackgroundColor = UIColor.Clear;
            //WebView.BackgroundColor = UIColor.Clear;
    
            //WebView.ScrollView.ContentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentBehavior.Never;        
    

    您可以在我的WebViewController定义中看到我的一些失败尝试被注释掉。我试过插入物和其他东西

    1 回复  |  直到 8 年前
        1
  •  1
  •   Ax1le    8 年前

    您可以为此WebView设置正确的约束,使其变小。webview的顶部和底部约束应该连接到 TopLayoutGuide BottomLayoutGuide ,这样iphone x的“头”和“脚”就不用了。

    我将显示下面四个约束的故事板截图:

    enter image description here

    但是使用代码来创建约束可能更清楚:

    // Disable this property to enable autolayout
    MyWebView.TranslatesAutoresizingMaskIntoConstraints = false;
    
    var leadConstraint = NSLayoutConstraint.Create(MyWebView, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, View, NSLayoutAttribute.Leading, 1.0f, 0);
    var topConstraint = NSLayoutConstraint.Create(MyWebView, NSLayoutAttribute.Top, NSLayoutRelation.Equal, TopLayoutGuide, NSLayoutAttribute.Bottom, 1.0f, 0);
    var trailingConstraint = NSLayoutConstraint.Create(MyWebView, NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, View, NSLayoutAttribute.Trailing, 1.0f, 0);
    var bottomConstraint = NSLayoutConstraint.Create(MyWebView, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, BottomLayoutGuide, NSLayoutAttribute.Top, 1.0f, 0);
    
    View.AddConstraints(new NSLayoutConstraint[] { leadConstraint, topConstraint, trailingConstraint, bottomConstraint });
    

    这将在iphone x的顶部和底部留出一个空间,我们不应该使用它,我们称之为 安全区域 .

    如果要使其看起来像WebView的组件,可以设置 View 的背景色与您的WebView的相同。从屏幕截图上看,它似乎是紫红色的。可能是这样的:

    View.BackgroundColor = UIColor.Purple;