如何以 xamarin 形式显示下拉列表.IOS

编程入门 行业动态 更新时间:2024-10-19 12:42:07
本文介绍了如何以 xamarin 形式显示下拉列表.IOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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:关键词]

本文发布于:2023-04-24 18:31:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1068497.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:形式   列表   xamarin   IOS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!