macOS上的WKWebView绝顶 [英] WKWebView on macOS cuts off top

查看:855
本文介绍了macOS上的WKWebView绝顶的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将WKWebView放置在y坐标> 0的NSView内.加载页面(无论是哪个页面)后,它要么立即切掉顶部,要么显示顶部一秒钟,然后向下跳转页面(通过执行以下操作)所以剪掉顶部).

I placed a WKWebView inside a NSView with a y coordinate > 0. After loading a page (no matter which one) it either immediately cuts off the top or it shows the top for a second and then jumps down the page (by doing so cutting off the top).

更重要的是,当我向上滚动时,我可以瞥见顶部,但它会弹回来,不允许我实际滚动到最顶部.

What's more is that when I scroll up I get a glimpse of the top portion but it bounces back not allowing me to actually scroll to the very top.

未对WKWebView进行任何更改.我注意到的是,y越小,被切除的部分就越小.

No changes have been made to the WKWebView. What I noticed is, that the smaller y the smaller the portion which is cut off.

WKWebView已通过Interface Builder添加.

The WKWebView has been added via the Interface Builder.

如何使WKWebView显示在网站顶部?我正在使用Swift.

How can I make the WKWebView show the top of the website? I'm using Swift.

这是它的样子:

这是实际网站的外观(请注意可见的导航部分):

This is what the actual website looks like (notice the visible navigation section):

推荐答案

好的,我和您有同样的问题!检查构造视图的框架,如果它不是从0,0开始,请确保父视图不会自动调整子视图的大小.我试图将代码示例保持在最低限度. (请注意,此示例中不需要webConfig.AllowsAirPlayForMediaPlayback)

Okay, I had the same problem as you! Check your view's frame on construction and, if it's not starting at 0,0, make sure that the parent view doesn't auto resize the subviews. I tried to keep my code sample to the bare minimum. (note that webConfig.AllowsAirPlayForMediaPlayback isn't required in this sample)

这是一个可行的例子.您可以在窗口中将视图切换为全屏视图,或在父视图中将其定位为较小的视图.我在IB中将窗口设置为1024x768.

Here's a working example. You can switch the view to be full frame in the window or positioned as a smaller view in the parent. I set the window to be 1024x768 in IB.

您可以轻松地将此代码转换为Swift.

You can easily convert this code to Swift.

关键是不要将子视图的大小自动调整为非0.0的位置.

The key is to not autoresize the subviews if the view is being placed at an offset other than 0,0.

快速样本:

//#define USE_FULL_WINDOW // uncomment this to fit webview full window

using System;
using AppKit;
using Foundation;
using WebKit;
using CoreGraphics;
using System.Diagnostics;

namespace WkWebTest
{
    public class MediaView : NSView
    {
        private WKWebView webView;

        public MediaView(CGRect frame)
            : base(frame)
        {
#if USE_FULL_WINDOW
            this.AutoresizesSubviews = true;
            this.AutoresizingMask = NSViewResizingMask.HeightSizable | NSViewResizingMask.WidthSizable;
#endif

            WKWebViewConfiguration webConfig = new WKWebViewConfiguration();
            webConfig.AllowsAirPlayForMediaPlayback = true;
            this.webView = new WKWebView(new CGRect(0, 0, frame.Width, frame.Height), webConfig);
            this.webView.AutoresizesSubviews = true;
            this.webView.AutoresizingMask = NSViewResizingMask.HeightSizable | NSViewResizingMask.WidthSizable;
            this.AddSubview(this.webView);

            string url = "http://www.apple.com";
            Debug.WriteLine("LoadUrl: " + url);
            var request = new NSUrlRequest(new NSUrl(url));
            this.webView.LoadRequest(request);
        }

        public override bool IsFlipped { get { return true; } }
    }

    public partial class ViewController : NSViewController
    {
        private MediaView mediaView;

        public ViewController(IntPtr handle) : base(handle)
        {
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

#if USE_FULL_WINDOW
            float mediaViewLeft = 0;
            float mediaViewTop = 0;
            nfloat mediaViewWidth = View.Frame.Width;
            nfloat mediaViewHeight = View.Frame.Height;
#else
            float mediaViewLeft = 511;
            float mediaViewTop = 112;
            nfloat mediaViewWidth = 475;
            nfloat mediaViewHeight = 548;
#endif
            this.mediaView = new MediaView(new CGRect(mediaViewLeft, mediaViewTop, mediaViewWidth, mediaViewHeight));
            this.View.AddSubview(mediaView);
        }

// boiler plate code generated by Xamarin
        public override NSObject RepresentedObject
        {
            get
            {
                return base.RepresentedObject;
            }
            set
            {
                base.RepresentedObject = value;
                // Update the view, if already loaded.
            }
        }
    }
}

这篇关于macOS上的WKWebView绝顶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆