首页 > 解决方案 > 是否可以调整 UITabbarcontroller 的子视图的大小?

问题描述

所以我得到了一个这样的故事板布局:

UITabbarcontroller -> child1 -> child2

我能够在 child2 的 viewdidload 方法中的 UITabBar 上方动态添加状态栏,如下所示

    StatusBar.BackgroundColor = Appearance.ErrorColor.BackgroundColor;
    var window = UIApplication.SharedApplication.Delegate.GetWindow();
    window.AddSubview(StatusBar);

现在的问题是,当我说状态栏存在时,我希望任何孩子的内容都向上移动

我尝试了很多排列和方法,包括增加状态栏的大小和减小子视图的大小,但无济于事。帮助将不胜感激!

    UIView.Animate(0.2, () => {
        var frame = this.View.Frame;
        this.View.Frame = new CoreGraphics.CGRect(frame.X, frame.Y, frame.Width, frame.Height - 40);

        this.View.InvalidateIntrinsicContentSize();
        this.View.LayoutIfNeeded();
        this.View.SetNeedsLayout();

    });

标签: c#xamarinxamarin.ios

解决方案


可能,但是前提是view的Y坐标正好是40。当你隐藏状态栏时,Y坐标变成0,整个View会向上移动。

通常,Tabview 的子视图的XY坐标从 开始(0,0),而不是从开始(0,40),因此您下面的代码可能不起作用。

UIView.Animate(0.2, () => {
    var frame = this.View.Frame;
    this.View.Frame = new CoreGraphics.CGRect(frame.X, frame.Y, frame.Width, frame.Height - 40);

    this.View.InvalidateIntrinsicContentSize();
    this.View.LayoutIfNeeded();
    this.View.SetNeedsLayout();

});

但是代码没问题,只要确保子视图的大小从(0,40)开始,就可以看到效果了。

=======================================更新============ =======================

如果您想要转换视图,编码如下:

UIView.Animate(2, () =>
{
    var frame = this.View.Frame;
    this.View.Transform = new CoreGraphics.CGAffineTransform((System.nfloat)0.8, 0, 0, (System.nfloat)0.8, 0, 0);
    //change contained parameter can modify the level of transform , and even can back to normal status
    this.View.InvalidateIntrinsicContentSize();
    this.View.LayoutIfNeeded();
    this.View.SetNeedsLayout();
});

恢复正常状态,替换为:

this.View.Transform = new CoreGraphics.CGAffineTransform(1, 0, 0, 1, 0, 0);

推荐阅读