ios - 如何为 Web 视图应用程序添加上边距?
问题描述
我有一个带有顶部菜单栏的 Web 视图应用程序。在 Iphone X 之前一切正常。如您所见,它有一个顶部菜单栏,顶部需要一些空间来清除 iphone X 的扬声器。有什么办法可以使网页视图更小?我尝试了约束,但我无法让它正常工作。
我可以在顶部缩小 webview 并添加一些背景颜色/渐变或其他东西吗?正如您在图片中看到的那样,当我向下滚动时,我可以看到扬声器两侧和菜单栏上方的内容(应该在 web 视图的最顶部)
这是我的类定义的开始:
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 定义中看到我的一些失败尝试已被注释掉。我尝试了 insents 和其他东西
解决方案
您可以为此 WebView 设置正确的约束以使其更小。WebView 的 top 和 bottom 约束应该连接到TopLayoutGuide
and BottomLayoutGuide
,这样 iPhone X 的“head”和“foot”就不会被使用。
我将在下面显示这四个约束的 Storyboard 屏幕截图:
但是使用代码创建约束可能更清楚:
// 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;
推荐阅读
- pyqt5 - Vtk 多重渲染交互 - 层上的交互
- r - curly curly 错误:未为 quosures 定义基本运算符
- angular - Firestore 活动文档快照侦听器中断排序
- azure-pipelines - 除了项目集合构建服务之外,是否还需要“允许项目范围的构建”
- system-verilog - 如何使用 $urandom_range 使我的 LED 以随机方式点亮?
- lua - 我将如何更改 roblox 中仅一个用户的颜色
- jenkins - 如何使用多个 jenkinsfile 创建多分支管道?
- c - 在 C 中的 sa_handler 中阻塞 SIGINT
- python - 如何在电子邮件中发送用 python 保存的 pdf?
- python - 如何使用python遍历文本文件(删除标签和规范化)