繁体   English   中英

如何在 Xamarin.iOS 中的明暗模式之间切换?

[英]How to toggle between Light & Dark Mode in Xamarin.iOS?

Most of the information I've found online is dealing with Xamarin Forms, Is it possible to toggle between Light & Dark Mode using only Xamarin.iOS?

我已将这段代码添加到我的一个控制器中的 ViewDidLoad 方法中,以将其更改为暗模式,但我不知道如何将其切换回亮模式,如果有人可以分享一些关于如何做到这一点的想法吗?

public override void ViewDidLoad()
{
    UILabel fetchDataLabel;
    UIView loadingView;

    **this.OverrideUserInterfaceStyle = UIUserInterfaceStyle.Dark;**

    base.ViewDidLoad();

您可以覆盖TraitCollectionDidChange方法来检测OverrideUserInterfaceStyle的变化,然后在那里更新 UI:

public partial class ViewController : UIViewController
{
    public ViewController (IntPtr handle) : base (handle)
    {
    }

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();
        // Perform any additional setup after loading the view, typically from a nib.
        this.OverrideUserInterfaceStyle = UIUserInterfaceStyle.Dark;


        UIButton myButton = new UIButton(UIButtonType.System);
        myButton.Frame = new CGRect(25, 25, 300, 150);
        myButton.SetTitle("Change Dark Mode", UIControlState.Normal);
        myButton.TouchUpInside += (sender, e) => {

            this.OverrideUserInterfaceStyle = UIUserInterfaceStyle.Light;

        };

        View.Add(myButton);
    }

    public override void TraitCollectionDidChange(UITraitCollection previousTraitCollection)
    {
        base.TraitCollectionDidChange(previousTraitCollection);

        var UserInterfaceStyle = TraitCollection.UserInterfaceStyle;

        //Update your UI here
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM