我在网上找到的大部分信息都是关于Xamarin Forms的,是否可以只用Xamarin.iOS在Light & Dark Mode之间切换?
我在我的一个控制器中的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
}
}