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

推荐订阅源

阮一峰的网络日志
阮一峰的网络日志
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
Schneier on Security
The Last Watchdog
The Last Watchdog
Cyberwarzone
Cyberwarzone
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cyber Attacks, Cyber Crime and Cyber Security
L
Lohrmann on Cybersecurity
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
The Cloudflare Blog
V
V2EX
博客园_首页
博客园 - 聂微东
Vercel News
Vercel News
人人都是产品经理
人人都是产品经理
G
GRAHAM CLULEY
T
Tenable Blog
Last Week in AI
Last Week in AI
Y
Y Combinator Blog
L
LINUX DO - 最新话题
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
SecWiki News
SecWiki News
博客园 - 三生石上(FineUI控件)
S
Secure Thoughts
N
News | PayPal Newsroom
T
The Blog of Author Tim Ferriss
The GitHub Blog
The GitHub Blog
T
Troy Hunt's Blog
博客园 - 【当耐特】
Forbes - Security
Forbes - Security
H
Hacker News: Front Page
A
About on SuperTechFans
B
Blog RSS Feed
Engineering at Meta
Engineering at Meta
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
D
DataBreaches.Net
P
Privacy & Cybersecurity Law Blog
Schneier on Security
Schneier on Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Google DeepMind News
Google DeepMind News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Jina AI
Jina AI
D
Docker
P
Proofpoint News Feed

博客园 - 胖胖安

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