我正在使用ViewDeck并希望在.leftController
现在的问题是,如果我打开包含导航的左侧,右侧位与我的搜索字段重叠。
我通过过度写入摆脱了这一点UISearchBar
,文本字段将始终具有相同的宽度,但在一种情况下,ViewDeck 重叠,在另一种情况下,我隐藏了 ViewDeck 位,然后取消按钮将占用空间:
子类化 UISearchBar
#import "ViewDeckSearchBar.h"
#define kViewDeckPadding 55
@interface ViewDeckSearchBar()
@property (readonly) UITextField *textField;
@end
@implementation ViewDeckSearchBar
static CGRect initialTextFieldFrame;
- (void) layoutSubviews {
[super layoutSubviews];
// Store the initial frame for the the text field
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
initialTextFieldFrame = self.textField.frame;
});
[self updateTextFieldFrame];
}
-(void)updateTextFieldFrame{
int width = initialTextFieldFrame.size.width - (kViewDeckPadding + 6);
CGRect newFrame = CGRectMake (self.textField.frame.origin.x,
self.textField.frame.origin.y,
width,
self.textField.frame.size.height);
self.textField.frame = newFrame;
}
-(UITextField *)textField{
for (UIView *view in self.subviews) {
if ([view isKindOfClass: [UITextField class]]){
return (UITextField *)view;
}
}
return nil;
}
@end
视图控制器类
在我的 Navigation 类中,我需要覆盖这两个UISearchbarDelegate
方法才能使用搜索结果全屏显示:
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
[self.viewDeckController setLeftSize:0];
// I am also using scopes, which works fine (they fade out when not searching)
self.searchBar.scopeButtonTitles = @[@"Food",
@"Beverages",
@"Misc"];
}
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
self.viewDeckController.leftSize = 55;
}
结果
ViewDeck 显示在右侧:

(来源:减号网)
全屏搜索(按钮和范围按钮动画)。

(来源:减号网)