ios - 中心滚动视图对象到每个水平滚动页面 - Xamarin iOS
问题描述
我正在做与此非常相似的事情,https://www.youtube.com/watch?v=EwSTK24IQds,但是对于每个对象,我都放置了一个 UI 文本字段。当我使用不同的设备滚动浏览每个页面时,文本不会位于屏幕中间。无论设备是什么,我都必须怎么做才能使对象居中?用户正在填写表单并输入数据,然后滚动到每个字段的下一页。
谢谢
解决方案
原因:
您的文本字段在不同设备中的位置取决于您的文本字段的布局。
解决方案:
例如,您将 textField 添加到 ViewController 的默认视图。
无论设备是什么,我在这里提供了两种方法来使文本字段在视图中居中:
- 第一个是使用
Frame
。您可以计算不同设备中文本字段的坐标。 第二个是使用
Autolayout
。您可以向文本字段添加约束以定位其位置。public override void ViewDidLoad () { base.ViewDidLoad (); // Perform any additional setup after loading the view, typically from a nib. UITextField myTextView = new UITextField(); Add(myTextView); float ScreenWidth = (float)UIScreen.MainScreen.Bounds.Width; float ScreenHeight = (float)UIScreen.MainScreen.Bounds.Height; float textFieldWidth = 200; float textFieldHeight = 50; myTextView.Text = "center myTextView"; myTextView.TextAlignment = UITextAlignment.Center; myTextView.BackgroundColor = UIColor.Blue; //Method One: //Frame Center //myTextView.Frame = new CGRect(new CGPoint(View.Center.X-textFieldWidth/2,View.Center.Y-textFieldHeight/2), new CGSize(textFieldWidth, textFieldHeight)); //Frame //myTextView.Frame = new CGRect(ScreenWidth / 2 - textFieldWidth / 2, ScreenHeight / 2 - textFieldHeight / 2, textFieldWidth, textFieldHeight); //Method two: //autolayout //myTextView.TranslatesAutoresizingMaskIntoConstraints = false; //View.AddConstraint(NSLayoutConstraint.Create(myTextView, NSLayoutAttribute.Height, NSLayoutRelation.Equal, 1f, textFieldHeight)); //View.AddConstraint(NSLayoutConstraint.Create(myTextView, NSLayoutAttribute.Width, NSLayoutRelation.Equal, 1f, textFieldWidth)); //View.AddConstraint(NSLayoutConstraint.Create(myTextView, NSLayoutAttribute.CenterX, NSLayoutRelation.Equal, View, NSLayoutAttribute.CenterX, 1f, 10f)); //View.AddConstraint(NSLayoutConstraint.Create(myTextView, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, View, NSLayoutAttribute.CenterY, 1f, 10f)); }
此外,您可以在情节提要中添加约束:在情节 提要中居中文本字段
推荐阅读
- amazon-comprehend - 邮政编码未使用 AWS Comprehend 中的 PII 过滤器进行编辑
- reactjs - 错误:要使用需要 `addCSS` 的插件(例如关键帧、媒体查询),请将您的应用程序包装在 StyleRoot 中
- character-encoding - 读取 TCL 中的混合语言 txt 文件
- javascript - Stripe Connect - 为标准账户提供登录链接
- ruby-on-rails - 我可以使用 rails left_outer_join 查询,然后使用条件(如果存在)吗?
- firebase - 更新 Flutter Firebase 后收到 Bad State: field does not exist - QueryDocumentSnapshot
- java - 无法为片段中的 Viewpager 调用 getSupportFragmentMangager
- c++ - 减去两个 CV_64FC3 矩阵并保持负值
- r - 如何将函数应用于R中的所有列?
- sumifs - 基于日期范围的 Knime Sumifs