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

推荐订阅源

Forbes - Security
Forbes - Security
GbyAI
GbyAI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
SegmentFault 最新的问题
Y
Y Combinator Blog
Recorded Future
Recorded Future
博客园 - Franky
I
InfoQ
T
The Blog of Author Tim Ferriss
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
T
Tailwind CSS Blog
Cyberwarzone
Cyberwarzone
The Register - Security
The Register - Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
雷峰网
雷峰网
P
Palo Alto Networks Blog
G
GRAHAM CLULEY
Cloudbric
Cloudbric
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
MongoDB | Blog
MongoDB | Blog
F
Full Disclosure
Google DeepMind News
Google DeepMind News
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
Check Point Blog
爱范儿
爱范儿
The GitHub Blog
The GitHub Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
T
Threat Research - Cisco Blogs
U
Unit 42
N
Netflix TechBlog - Medium
The Cloudflare Blog
Spread Privacy
Spread Privacy
Microsoft Azure Blog
Microsoft Azure Blog
美团技术团队
T
Troy Hunt's Blog
Engineering at Meta
Engineering at Meta
H
Heimdal Security Blog
TaoSecurity Blog
TaoSecurity Blog
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tenable Blog
B
Blog
S
Securelist
H
Hacker News: Front Page
Google Online Security Blog
Google Online Security Blog
G
Google Developers Blog

博客园 - 胖胖安

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