惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
WordPress大学
WordPress大学
Google DeepMind News
Google DeepMind News
美团技术团队
大猫的无限游戏
大猫的无限游戏
H
Help Net Security
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
M
MIT News - Artificial intelligence
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - Franky
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Microsoft Security Blog
Microsoft Security Blog
F
Fortinet All Blogs
A
About on SuperTechFans
Recent Announcements
Recent Announcements
D
Docker
Vercel News
Vercel News
Engineering at Meta
Engineering at Meta
腾讯CDC
Martin Fowler
Martin Fowler
阮一峰的网络日志
阮一峰的网络日志

博客园 - 胖胖安

Xamarin.Android 的 Google 登入 Xamarin.iOS 的 Google 登入 Xamarin.Android 的照相機使用 Xamarin.iOS 照相機功能的使用 (1) :最簡單的做法 Xamarin.Android 控制鍵盤縮放 Xamarin 環境建置–Xamarin Agent install rvm iOS : TableView MultiSelect 兩個 System.IO.FileInfo 的 Extension Method SQL Server 2008 Resize LDF IIS 7 站點下,ASP.NET 3.5 與 4.0 的並存問題 jQuery AutoComplete Parameters Tips : SQL Server 2008 "saving changes is not permitted." 的解決辦法 - 胖胖安 Section 3 : 程式進入點 Section 2 : 專案結構 Section 1 : 使用 XCode 建立一個新專案 iPhone 開發前的準備工作 IIS 7 上 设定ASP.NET 时应注意事项 DBML 在Beta2與正式版間差異 - 胖胖安 - 博客园
Xamarin.iOS 的鍵盤控制 (AutoLayout 與 新的 Keyboard 事件 )
胖胖安 · 2016-03-29 · via 博客园 - 胖胖安

在 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