问题描述
限时送ChatGPT账号..这是我的 IOS iPad 页面我想显示下拉菜单,请帮助我如何编写代码,我为此使用了选择器,但菜单项显示在底部,我如何解决此问题
This Is My IOS IPad Page I want to display dropdown as show in image ,Pleases help me how can I write a code , I have use picker for that but the menu item is display on bottom ,How i resolve this issue
推荐答案
这里是实现这种下拉"的解决方法通过控制组合".
Here is a workaround that achieve such "Drop Down" via "control combination".
首先,在视图上添加一个 UITextField 来显示选中的项目.
First, add a UITextField on the view to show the item selected.
UITextField _dropTextField;
UIView _dropDownView;
UIPickerView _pickerView;
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Perform any additional setup after loading the view, typically from a nib.
this.View.BackgroundColor = UIColor.White;
// add textfield
_dropTextField = new UITextField();
_dropTextField.Frame = new CGRect(100, 100, 300, 30);
_dropTextField.BackgroundColor = UIColor.Gray;
this.View.AddSubview(_dropTextField);
// call CreateDropDownView
CreateDropDownView();
}
其次,定义一个方法来创建一个自定义的 UIView 持有一个 UIPickerView.
Second, define a method that create a custom UIView holds a UIPickerView.
// create a custom UIView to show pickView
private void CreateDropDownView()
{
_dropDownView = new UIView(new CGRect(_dropTextField.Frame.X, _dropTextField.Frame.Y,
_dropTextField.Frame.Width, 300));
_dropTextField.Delegate = this;
_pickerView = new UIPickerView();
_pickerView.Frame = new CGRect(_dropTextField.Bounds.X, _dropTextField.Bounds.Y + _dropTextField.Frame.Height, _dropTextField.Frame.Width, 300);
PeopleModel pickerModel = new PeopleModel(_dropTextField, _dropDownView);
_pickerView.Model = pickerModel;
_dropDownView.AddSubview(_pickerView);
}
然后,要在TextField聚焦时显示UIView,还需要实现接口IUITextFieldDelegate
.
Then, to show the UIView when TextField focused, you also need to implement interface IUITextFieldDelegate
.
// show pickerview
[Export("textFieldShouldBeginEditing:")]
public bool ShouldBeginEditing(UITextField textField)
{
View.AddSubview(_dropDownView);
UIApplication.SharedApplication.KeyWindow.BringSubviewToFront(_pickerView);
return false;
}
关于上面代码中的PeopleModel
,请参考class PeopleModel";在此文档中.此外,您需要覆盖其中的一些方法.
As for the PeopleModel
in the above code, please refer to "class PeopleModel" in this documentation. Also, you need to override some method in it.
private UITextField dropTextField;
private UIView dropDownView;
public PeopleModel(UITextField dropTextField, UIView dropDownView)
{
this.dropDownView = dropDownView;
this.dropTextField = dropTextField;
}
public override nint GetComponentCount(UIPickerView pickerView)
{
return 1;
}
// ...
public override void Selected(UIPickerView pickerView, nint row, nint component)
{
dropDownView.RemoveFromSuperview();
dropTextField.Text = $"{names[pickerView.SelectedRowInComponent(0)]}";
}
// ...
这篇关于如何以 xamarin 形式显示下拉列表.IOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论