博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Xamarin.iOS 的鍵盤控制 (AutoLayout 與 新的 Keyboard 事件 )
阅读量:4618 次
发布时间:2019-06-09

本文共 2405 字,大约阅读时间需要 8 分钟。

在 iOS 增加了鍵盤類型之後,過去用來偵測鍵盤高度已調整 UITextField 位置的方法也需要改變。

加上目前的版面配置都是以 AutoLayout 為主。
此篇文章以這兩個前提為基礎撰寫。
(1) 使用 Storyboard 進行版面配置。
(2) 在 Storyboard 中,對 UITextField 新增 NSLayoutConstraint,控制 UITextField 下方間距。
(3) 註冊兩個事件。一為  WillChangeFrameNotification, 一為  WillChangeFrameNotification。
   透過這兩個事件,改變控制 UITextField 下方間距的 NSLayoutConstraint,達成控制 UITextField 位置的目標。
以下是操作過程:
(1) 在 Xamarin Studio 的專案中,指定以 Xcode 開啟Storyboard
    
    

(2) 在 UIViewController 的 View 中,將 UITextField 與 UIButton 配置於 View 的下緣。

    透過這個配置,展現如果不移動位置,使用者將難以輸入。

(3) 指定 UITextField 下緣對齊的 NSLayoutConstraint。讓程式可以控制 UITextField 的下緣距離。

    
(4) 在程式中的 ViewDidLoad 內

public override void ViewDidLoad ()

 註冊兩個 Keyboard 事件。

NSNotificationCenter.DefaultCenter.AddObserver( UIKeyboard.WillChangeFrameNotification, UIKeyboardWillChangeFrameNotification);NSNotificationCenter.DefaultCenter.AddObserver( UIKeyboard.WillHideNotification, UIKeyboardWillHideNotification);

 

 

並且在這兩個方法內控制之前所提的 NSLayoutConstraint 的 Constant 值。

private void UIKeyboardWillHideNotification (NSNotification notification){            NSString key = new NSString(UIKeyboard.FrameEndUserInfoKey.ToString());            NSObject objRect = notification.UserInfo[key];            if (objRect is NSValue) {                var v = (NSValue)objRect;                var rect = v.RectangleFValue;                _currentKeyboardHight = (int)rect.Height;                Debug.WriteLine ("Hide Keyboard Height:{0}", _currentKeyboardHight);                txtUrlBottomConstraint.Constant = 5;            }        }        private void UIKeyboardWillChangeFrameNotification(NSNotification notification){            if(notification.UserInfo.ContainsKey( new NSString(UIKeyboard.FrameBeginUserInfoKey.ToString()))){                NSString key = new NSString(UIKeyboard.FrameEndUserInfoKey.ToString());                NSObject objRect = notification.UserInfo[key];                if (objRect is NSValue) {                    var v = (NSValue)objRect;                    var rect = v.RectangleFValue;                    _currentKeyboardHight = (int)rect.Height;                    Debug.WriteLine ("Change Keyboard Height:{0}", _currentKeyboardHight);                    txtUrlBottomConstraint.Constant = _currentKeyboardHight + 5;                }            }        }

 

(5) 進行測試,即可發現 UITextField 位置由 NSLayoutConstraint 控制

程式碼位置:https://github.com/FangHuaiAn/Xamarin-iOSTips

转载于:https://www.cnblogs.com/Liddle/p/5329490.html

你可能感兴趣的文章
Python标准库:内置函数getattr(object, name[, default])
查看>>
转:android 自定义RadioButton样式
查看>>
HTTP请求过程
查看>>
织梦多域名解析到同一个空间导致打开链接不一致怎么办?
查看>>
Xcode10 library not found for -lstdc++ 找不到问题
查看>>
Mysql 8.0.13如何重置密码
查看>>
发布功能完成
查看>>
excel 合并单元格
查看>>
iOS设计模式简介
查看>>
c# 扩展方法 奇思妙用 高级篇 九:OrderBy(string propertyName, bool desc)
查看>>
C语言中的地址传递(传指针,传递给形参的指针仍然是实参指针的一份拷贝)
查看>>
redis缓存数据库及Python操作redis
查看>>
opencms忘记Admin用户登录密码解决方案
查看>>
forms组件
查看>>
create-react-app 配置sass
查看>>
02_关系数据库
查看>>
在win7电脑中如何查看运行进程的PID标识符
查看>>
[Vue] vue-cli3.0安装
查看>>
shell中如何进行算术运算
查看>>
为什么所有的架构都是糟糕的
查看>>