iOS TabbarController 覆盖内容

Posted

技术标签:

【中文标题】iOS TabbarController 覆盖内容【英文标题】:iOS TabbarController overlays content 【发布时间】:2018-04-07 22:40:30 【问题描述】:

我有一个 TabBarController,可以在其中显示不同的页面。问题是,页面底部的 TabBar 不限制选项卡内内容的大小,而是覆盖它。有没有办法改变这种行为。更多信息:

我使用 Xamarin.ios,但要启动 TabBarController(使用 Storyboard),但选项卡的内容是 Xamarin Forms ContentPages。 (本机 Xamarin 表单) 这是我的问题的图片

编辑 1 这是我用来初始化 XF ContentPage 的代码:

namespace Happimeter.iOS

    public partial class SurveyViewController : UINavigationController
    
        public SurveyViewController (IntPtr handle) : base (handle)
        
            //InitializeSurveyView is a XF ContentPage
            var formsPage = new InitializeSurveyView();
            var startSurveyVc = formsPage.CreateViewController();
            PushViewController(startSurveyVc, true);
            startSurveyVc.Title = formsPage.Title;

            //some code omitted for brevity

            NavigationBar.TintColor = UIColor.White;
        
    

我将 viewController 分配给 Storyboard 中的 tabBarController。见下文:

Main.storyboard:

<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13771" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="49e-Tb-3d3" useSafeAreas="YES">
<device id="retina4_7" orientation="portrait">
    <adaptation id="fullscreen"/>
</device>
<dependencies>
    <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13772"/>
    <capability name="Constraints to layout margins" minToolsVersion="6.0"/>
    <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
    <capability name="Safe area layout guides" minToolsVersion="9.0"/>
</dependencies>
<customFonts key="customFonts">
    <array key="HelveticaNeue.ttc">
        <string>HelveticaNeue</string>
    </array>
</customFonts>
<scenes>
    <!--About-->
    <!--Tab Bar Controller-->
    <scene sceneID="yl2-sM-qoP">
        <objects>
            <tabBarController storyboardIdentifier="tabViewController" id="49e-Tb-3d3" customClass="TabBarController" sceneMemberID="viewController">
                <nil key="simulatedBottomBarMetrics"/>
                <tabBar key="tabBar" contentMode="scaleToFill" id="W28-zg-YXA" clipsSubviews="YES">
                    <rect key="frame" x="0.0" y="618"  />
                    <autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES"/>
                    <color key="backgroundColor" red="0.0" green="0.0" blue="0.0" alpha="0.0" colorSpace="custom" customColorSpace="sRGB"/>
                </tabBar>
                <connections>
                    <segue id="83046" destination="82960" kind="relationship" relationship="viewControllers"/>
                    <segue id="91417" destination="91401" kind="relationship" relationship="viewControllers"/>
                    <segue id="96651" destination="96631" kind="relationship" relationship="viewControllers"/>
                    <segue id="100501" destination="95834" kind="relationship" relationship="viewControllers"/>
                </connections>
            </tabBarController>
            <placeholder placeholderIdentifier="IBFirstResponder" id="HuB-VB-40B" sceneMemberID="firstResponder"/>
        </objects>
        <point key="canvasLocation" x="0.0" y="0.0"/>
    </scene>
    <!--Browse-->
    <!--About-->
    <!--Items-->
    <!--Title-->
    <!--New Item-->
    <!--Login View Controller-->
    <!--Login View Controller-->
    <!--Login View Controller-->
    <!--View Controller-->
    <scene sceneID="82959">
        <objects>
            <navigationController id="82960" sceneMemberID="viewController" customClass="SurveyViewController">
                <navigationBar key="navigationBar" contentMode="scaleToFill" id="82962" translucent="NO">
                    <rect key="frame" x="0.0" y="20"  />
                    <autoresizingMask key="autoresizingMask"/>
                    <color key="barTintColor" colorSpace="calibratedRGB" red="0.18823529411764706" green="0.47843137254901963" blue="0.91764705882352937" alpha="1"/>
                    <textAttributes key="titleTextAttributes">
                        <color key="textColor" colorSpace="calibratedWhite" white="1" alpha="1"/>
                    </textAttributes>
                </navigationBar>
                <connections/>
                <tabBarItem key="tabBarItem" title="Item" id="83045"/>
                <simulatedTabBarMetrics key="simulatedBottomBarMetrics"/>
            </navigationController>
            <placeholder placeholderIdentifier="IBFirstResponder" id="82963" userLabel="First Responder" sceneMemberID="firstResponder"/>
        </objects>
        <point key="canvasLocation" x="-495" y="794"/>
    </scene>
    <scene sceneID="91400">
        <objects>
            <navigationController id="91401" sceneMemberID="viewController" customClass="MoodOverviewViewController">
                <navigationBar key="navigationBar" contentMode="scaleToFill" id="91403" translucent="NO">
                    <rect key="frame" x="0.0" y="20"  />
                    <autoresizingMask key="autoresizingMask"/>
                    <color key="barTintColor" colorSpace="calibratedRGB" red="0.18823529411764706" green="0.47843137254901963" blue="0.91764705882352937" alpha="1"/>
                    <textAttributes key="titleTextAttributes">
                        <color key="textColor" colorSpace="calibratedWhite" white="1" alpha="1"/>
                    </textAttributes>
                </navigationBar>
                <connections/>
                <tabBarItem key="tabBarItem" title="Item" id="91402"/>
                <simulatedTabBarMetrics key="simulatedBottomBarMetrics"/>
            </navigationController>
            <placeholder placeholderIdentifier="IBFirstResponder" id="91404" userLabel="First Responder" sceneMemberID="firstResponder"/>
        </objects>
        <point key="canvasLocation" x="726" y="-1558"/>
    </scene>
    <scene sceneID="95833">
        <objects>
            <navigationController id="95834" sceneMemberID="viewController" customClass="SettingsPageViewController">
                <navigationBar key="navigationBar" contentMode="scaleToFill" id="95836" translucent="NO">
                    <rect key="frame" x="0.0" y="20"  />
                    <autoresizingMask key="autoresizingMask"/>
                    <color key="barTintColor" colorSpace="calibratedRGB" red="0.18823529411764706" green="0.47843137254901963" blue="0.91764705882352937" alpha="1"/>
                    <textAttributes key="titleTextAttributes">
                        <color key="textColor" colorSpace="calibratedWhite" white="1" alpha="1"/>
                    </textAttributes>
                </navigationBar>
                <connections/>
                <tabBarItem key="tabBarItem" title="Item" id="95835"/>
                <simulatedTabBarMetrics key="simulatedBottomBarMetrics"/>
            </navigationController>
            <placeholder placeholderIdentifier="IBFirstResponder" id="95837" userLabel="First Responder" sceneMemberID="firstResponder"/>
        </objects>
        <point key="canvasLocation" x="959" y="-809"/>
    </scene>
    <scene sceneID="96630">
        <objects>
            <navigationController id="96631" sceneMemberID="viewController" customClass="BluetoothPageViewController">
                <navigationBar key="navigationBar" contentMode="scaleToFill" id="96633" translucent="NO">
                    <rect key="frame" x="0.0" y="20"  />
                    <autoresizingMask key="autoresizingMask"/>
                    <color key="barTintColor" colorSpace="calibratedRGB" red="0.18823529411764706" green="0.47843137254901963" blue="0.91764705882352937" alpha="1"/>
                    <textAttributes key="titleTextAttributes">
                        <color key="textColor" colorSpace="calibratedWhite" white="1" alpha="1"/>
                    </textAttributes>
                </navigationBar>
                <connections/>
                <tabBarItem key="tabBarItem" title="Item" id="96632"/>
                <simulatedTabBarMetrics key="simulatedBottomBarMetrics"/>
            </navigationController>
            <placeholder placeholderIdentifier="IBFirstResponder" id="96634" userLabel="First Responder" sceneMemberID="firstResponder"/>
        </objects>
        <point key="canvasLocation" x="1350" y="-809"/>
    </scene>
    <scene sceneID="97944">
        <objects>
            <navigationController id="97945" sceneMemberID="viewController" customClass="SignInViewController" storyboardIdentifier="SignInViewController">
                <navigationBar key="navigationBar" contentMode="scaleToFill" id="97947" translucent="NO">
                    <rect key="frame" x="0.0" y="20"  />
                    <autoresizingMask key="autoresizingMask"/>
                    <color key="barTintColor" colorSpace="calibratedRGB" red="0.18823529411764706" green="0.47843137254901963" blue="0.91764705882352937" alpha="1"/>
                    <textAttributes key="titleTextAttributes">
                        <color key="textColor" colorSpace="calibratedWhite" white="1" alpha="1"/>
                    </textAttributes>
                </navigationBar>
                <connections/>
                <tabBarItem key="tabBarItem" title="Item" id="97946"/>
                <simulatedTabBarMetrics key="simulatedBottomBarMetrics"/>
            </navigationController>
            <placeholder placeholderIdentifier="IBFirstResponder" id="97948" userLabel="First Responder" sceneMemberID="firstResponder"/>
        </objects>
        <point key="canvasLocation" x="2905" y="-16"/>
    </scene>
</scenes>
<resources>
    <image name="First"  />
    <image name="HappimeterLogo"  />
    <image name="Second"  />
    <image name="XamarinLogo"  />
    <image name="happinessfactory"  />
</resources>

【问题讨论】:

你在使用"safe area" layout guides? 它被关闭了,我把它打开了。可惜效果还是一样。不管怎样,谢谢你的建议 很明显 XF ContentPage 在渲染时没有考虑 TabBar 的高度。您能否更新您的帖子以显示您如何嵌入 XF ContentPage? @user2074945 请分享一些关于如何使用 Xamarin Forms ContentPages 设置 TabBarController 的子视图的代码。 感谢您的 cmets。我编辑了问题,提供了我使用的代码初始化内容页并将其分配给 tabbarController 【参考方案1】:
public partial class TabBarController : UITabBarController

    public TabBarController(IntPtr handle) : base(handle)
    
        //this does the trick!
        TabBar.Translucent = false;
    

【讨论】:

【参考方案2】:

这对我有用:

class TabBarController: UITabBarController 

override func viewDidLoad() 
    super.viewDidLoad()
    
    tabBar.isTranslucent = false

【讨论】:

以上是关于iOS TabbarController 覆盖内容的主要内容,如果未能解决你的问题,请参考以下文章

iOS 自定义TabBarController zhuanzai

iOS TabBarController 只显示黑屏

iOS 自定义TabBarController

iOS:CYLTabBarController低耦合集成TabBarController

iOS 7 - TabBarController 没有文本标签

iOS 项目架构tabbarController 嵌套 navbarController