iOS定制复杂UI

文章出处,原创于 https://HawkingOuYang.github.io/

我的GitHub


键盘

iOS inputView 和 inputAccessoryView
http://blog.csdn.net/nyh1006/article/details/18735819

// #import “HTmSearchBar.h”

1
2
3
4
5
@interface HTmSearchBar ()
{
BOOL m_isSet_InputView;
}
@end

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
@implementation HTmSearchBar
//! inputView属性 (2012年的iOS系统有这个属性,2016年的iOS系统没有这个属性)。
//@synthesize inputView=m_inputView;
/**
参考资料
Ref.:
How To Implement Custom keyboard with UISearchBar? (2012年的帖子,现在2016年 亲测不可行)
http://stackoverflow.com/questions/12805334/how-to-implement-custom-keyboard-with-uisearchbar
Customkeyboard Implemention with UISearchBar? (2013年的帖子,现在2016年 亲测不可行)
http://stackoverflow.com/questions/12843523/customkeyboard-implemention-with-uisearchbar
UITextField from UISearchBar in iOS 7.12014年的帖子,现在2016年 亲测可行!就用这个!)
http://stackoverflow.com/questions/22499379/uitextfield-from-uisearchbar-in-ios-7-1
iphone UISearchBar Done button always enabled (2011年的帖子,现在2016年 我没有测试)
http://stackoverflow.com/questions/4728338/iphone-uisearchbar-done-button-always-enabled
Getting reference to the top-most view/window in iOS application (可用作补充)
http://stackoverflow.com/questions/3843411/getting-reference-to-the-top-most-view-window-in-ios-application
*/
- (void)layoutSubviews
{
if (m_isSet_InputView == NO) {
m_isSet_InputView = YES;
/**
UPDATE
If your UISearchBar is in a UIViewController, try your code in method viewDidLoad or viewWillAppear!
Don't look for UITextField as soon as you create UISearchBar (look for after UISearchBar has already appeared on screen)
*/
/*
UITextField *textFieldInSearchBar = [HTmSearchBar p_findTextFieldFromControl:self];
textFieldInSearchBar.inputView = m_inputView;
*/
}
[super layoutSubviews];
}
#pragma mark - PUBLIC
// 从 view 的 视图层级中,找出 UITextField实例
+ (UITextField *)findTextFieldFromControl:(UIView *) view
{
DDLogInfo(@"%@,%@", THIS_FILE,THIS_METHOD);
//! 调用私有方法,注意这个 p_
return [HTmSearchBar p_findTextFieldFromControl:view];
}
#pragma mark - private
/*
//! 从 view 的 视图层级中,找出 UITextField实例
+ (UITextField *)p_findTextFieldFromControl:(UIView *) view
{//iOS 9 之前
for (UIView *subview in view.subviews)
{
if ([subview isKindOfClass:[UITextField class]])
{
return (UITextField *)subview;
}
else if ([subview.subviews count] > 0)
{
return [self findTextFieldFromControl:subview];
}
}
return nil;
}
*/
// 从 view 的 视图层级中,找出 UITextField实例
+ (UITextField *)p_findTextFieldFromControl:(UIView *) view
{//iOS 9 以及之后
DDLogInfo(@"%@,%@", THIS_FILE,THIS_METHOD);
/**
UITextField from UISearchBar in iOS 7.12014年的帖子,现在2016年 亲测可行!就用这个!)
http://stackoverflow.com/questions/22499379/uitextfield-from-uisearchbar-in-ios-7-1
关键:
Dint work for iOS 9 ... Probably Apple changed it again. Modified - (UITextField *) findTextFieldFromControl:(UIView *) view { int subViewIndex = 0; for (UIView *subview in view.subviews) { subViewIndex++; if ([subview isKindOfClass:[UITextField class]]) { return (UITextField *)subview; } else if (subViewIndex < view.subviews.count) { continue; } else if ([subview.subviews count] > 0) { subViewIndex = 0; return [self findTextFieldFromControl:subview]; } } return nil; }
*/
int subViewIndex = 0;
for (UIView *subview in view.subviews)
{
subViewIndex++;
if ([subview isKindOfClass:[UITextField class]])
{
return (UITextField *)subview;
}
else if (subViewIndex < view.subviews.count)
{
continue;
}
else if ([subview.subviews count] > 0)
{
subViewIndex = 0;
return [self p_findTextFieldFromControl:subview];
}
}
DDLogError(@"%@,%@ %@", THIS_FILE,THIS_METHOD,@"在UISearchBar的视图层级中,没找到UITextField");
return nil;
}
/*
使得 UISearchBarDone 按钮 总是可以点击
iphone UISearchBar Done button always enabled
// loop around subviews of UISearchBar
for (UIView *searchBarSubview in [sourceView subviews]) {
if ([searchBarSubview conformsToProtocol:@protocol(UITextInputTraits)]) {
@try {
// set style of keyboard
[(UITextField *)searchBarSubview setKeyboardAppearance:UIKeyboardAppearanceAlert];
// always force return key to be enabled
[(UITextField *)searchBarSubview setEnablesReturnKeyAutomatically:NO];
}
@catch (NSException * e) {
// ignore exception
}
}
}
*/
@end