- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将 MySearchBar
(UISearchBar
添加了 UILabel
来显示标题)显示为 navigationItem
> 观点:
// Make search bar autoresize and occupy maximum width
self.navigationItem.titleView = [[UIView alloc] initWithFrame:self.navigationController.navigationBar.bounds];
self.navigationItem.titleView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.navigationItem.titleView addSubview:self.mySearchBar.view];
self.mySearchBar.view.frame = self.navigationItem.titleView.frame;
搜索栏会一直保持在原位,直到被点击为止。点击后,它向下移动,不再占据整个宽度(大约缺少 16px)。
编辑之前(应该是什么样子):
编辑开始(BUG - 点击搜索栏后,高度会增加到默认的 56px 并失去宽度):
编辑结束(仍然错位)
这里是MySearchBar
的初始化代码。它由两个主要 View 组成,titleView
用标题包装 UILabel
,searchBarWrapperView
包装 UISearchBar
:
-(id) init {
// Initialize the two wrapping views
UIView *titleView = [UIView new];
self.searchBarWrapperView = [UIView new];
// Resize UILabel to fit its content
self.titleLabel = [UILabel new];
self.titleLabel.numberOfLines = 0;
[self.titleLabel sizeToFit];
self.searchBar = self.searchController.searchBar;
self.searchBarTextField = [self.searchBar valueForKey:@"searchField"];// This could be the text field that gets displaced???
// Add two main subviews (wrappers)
[self.view addSubview:titleView];
[self.view addSubview:self.searchBarWrapperView];
// Add title label and search bar to the subviews
[titleView addSubview:self.titleLabel];
[self.searchBarWrapperView addSubview:self.searchBar];
// Disable auto constraints
[titleView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.searchBarWrapperView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.titleLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
// Set constraints for title view
[titleView.topAnchor constraintEqualToAnchor:self.view.topAnchor].active = YES;
[titleView.leftAnchor constraintEqualToAnchor:self.view.leftAnchor].active = YES;
[titleView.rightAnchor constraintEqualToAnchor:self.view.rightAnchor].active = YES;
[titleView.bottomAnchor constraintEqualToAnchor:self.titleLabel.bottomAnchor].active = YES;
// Set constraints for title label
[self.titleLabel.topAnchor constraintEqualToAnchor:titleView.topAnchor].active = YES;
[self.titleLabel.leftAnchor constraintEqualToAnchor:titleView.leftAnchor].active = YES;
[self.titleLabel.rightAnchor constraintEqualToAnchor:titleView.rightAnchor].active = YES;
// Set constraints for search bar wrapper
[self.searchBarWrapperView.heightAnchor constraintGreaterThanOrEqualToConstant:30].active = YES;// The search bar together with the title label should occupy the whole 44px height of the navigation bar
[self.searchBarWrapperView.topAnchor constraintEqualToAnchor:titleView.bottomAnchor].active = YES;
[self.searchBarWrapperView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor].active = YES;
[self.searchBarWrapperView.leftAnchor constraintEqualToAnchor:self.view.leftAnchor].active = YES;
[self.searchBarWrapperView.rightAnchor constraintEqualToAnchor:self.view.rightAnchor constant:16].active = YES;// Add space that is lost by setSearchFieldBackgroundPositionAdjustment
// Configure autoresizing mask for UISearchBar
[self.searchBar setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[self.searchBar setFrame:self.searchBarWrapperView.frame];
//Remove left and right blank spaces around UISearchBar
[self.searchBar setSearchFieldBackgroundPositionAdjustment:UIOffsetMake(-8,0)];
// Colors for debugging
self.view.backgroundColor = [UIColor blueColor];
titleView.backgroundColor = [UIColor redColor];
searchBarWrapperView.backgroundColor = [UIColor greenColor];
self.searchBar.backgroundColor = [UIColor yellowColor];
self.searchBarTextField.backgroundColor = [UIColor brownColor];
return self;
}
最佳答案
我使用的解决方案是子类化 UISearchBar
和 UISearchController
。这样我就可以设置框架并控制搜索栏事件。
SearchBar.swift
:
import Foundation
class SearchBar : UISearchBar {
var preferredFont:UIFont?
var preferredTextColor:UIColor?
init(frame: CGRect, font: UIFont, textColor: UIColor) {
super.init(frame: frame)
self.frame = frame
self.preferredFont = font
self.preferredTextColor = textColor
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
}
SearchController.swift
:
import Foundation
protocol SearchControllerDelegate {
func didStartSearching()
func didTapOnSearchButton()
func didTapOnCancelButton()
func didChangeSearchText(searchText: String)
}
class SearchController : UISearchController, UISearchBarDelegate, SearchControllerDelegate {
var customSearchBar: SearchBar!
var customDelegate: SearchControllerDelegate!
init(searchResultsController: UIViewController!, searchBarFrame: CGRect, searchBarFont: UIFont, searchBarTextColor: UIColor, searchBarTintColor: UIColor) {
super.init(searchResultsController: searchResultsController)
configureSearchBar(frame: searchBarFrame, font: searchBarFont, textColor: searchBarTextColor, bgColor: searchBarTintColor)
}
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
func configureSearchBar(frame: CGRect, font: UIFont, textColor: UIColor, bgColor: UIColor) {
self.customSearchBar = SearchBar(frame: frame, font: font , textColor: textColor)
self.customSearchBar.placeholder = "Search"
self.customSearchBar.barTintColor = bgColor
self.customSearchBar.tintColor = textColor
self.customSearchBar.showsBookmarkButton = false
self.customSearchBar.showsCancelButton = false
self.customSearchBar.delegate = self
self.customDelegate = self;
let searchBarTextField:UITextField = self.customSearchBar.value(forKey: "searchField") as! UITextField
searchBarTextField.font = font
searchBarTextField.layer.borderWidth = 1
searchBarTextField.layer.cornerRadius = 3
searchBarTextField.layer.borderColor = UIColor.lightGray.cgColor
}
// UISearchBarDelegate
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
customDelegate.didStartSearching()
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
customSearchBar.resignFirstResponder()
customDelegate.didTapOnSearchButton()
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
customSearchBar.resignFirstResponder()
customDelegate.didTapOnCancelButton()
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
customDelegate.didChangeSearchText(searchText: searchText)
}
// SearchControllerDelegate
func didStartSearching() {
}
func didTapOnSearchButton() {
var searchText:String = ""
if (self.customSearchBar.text != nil) {
searchText = self.customSearchBar.text!
}
self.search(searchQuery: searchText)
}
func didTapOnCancelButton() {
}
func didChangeSearchText(searchText: String) {
self.search(searchQuery: searchText)
}
// Search
func search(searchQuery: String) {
// Start searching
}
}
关于ios - UISearchBar 一旦被点击就会跳出位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52097784/
这个问题本质上是,UISearchBar在激活时与状态栏重叠。我知道已经有很多类似的问题发布了,我已经尝试了所有方法,但仍然无法解决。 当屏幕初始化时,搜索栏似乎位于正确的位置。当我向下滚动屏幕时,您
我尝试在我的应用程序中实现 iOS 11 中的新搜索栏,但出于某种原因,我无法让搜索栏颜色随导航栏一起更改。在切换到新样式之前,我在 AppDelagate 中使用 UISearchBar.appea
我正在努力做到这一点: Consider a table view with Search Bar on its header.Table view gets refreshed whenever s
documentation表示 UISearchBar我们可以在 tvOS 中使用它(Apple 当然在他们的搜索 View 中使用它),但我在对象库中找不到它。 其他人有同样的问题吗?我没有看到一个
当我在 UISearchBar 上搜索时,我的普通 tableView 已设置为 - (UITableViewCell *)tableView:(UITableView *)tableView cel
这里是我在view controller.h中的代码 #import "AppDelegate.h" @interface SearchRsultsFanSideViewController : UI
我在一个应用程序工作,我今天在 iOS 7.1 上进行了测试。到目前为止,我的搜索栏是正常的: 但现在我有这个问题: 出现一个灰色 View ,我不知道如何删除它,因为我的代码中没有这个 View 。
我正在更新适用于 iOS 7 的应用程序,而我的一项功能(允许在搜索栏中没有文本的情况下激活搜索栏搜索按钮)停止工作。我使用了以下代码。关于如何让它再次工作有什么建议吗?提前致谢。 UITextFie
我有一个带有 UISearchBar(和 SearchDisplayController)的 UIViewController 以及一个 UITableView。当导航到这个 View Control
我有一个我似乎无法克服的小情况。 我有一个 UITableView,它的顶部嵌入了一个 UISearchBar。 TableView 由 NSFetchedResultsController 和 Co
我在 TableView 顶部使用带有 UISearchBar 的 UITableView,对我来说,一切都工作正常,只有一个问题,比如如果我搜索一个字母,uitableview 中的搜索结果被过滤,
表格标题中有一个搜索栏。当用户在 iOS 7 上快速点击它两次时,它就会消失。有人对我们做错了什么有什么建议吗? 最佳答案 经过大量的试验和错误,我发现当searchDisplayController
我想更改 UISearchBar。文本字段的高度和宽度。我的问题是如何更改 iphone 中 UISearchBar 中的 UiSearchbar 高度、宽度、颜色 和 Uitextfield 高度?
这两张图应该可以说明了吧。第一个正常显示我的 UITableView。第二个显示当我触摸 UISearchBar 并且它获得焦点时。为什么单元格标题和搜索栏之间有间隙?有人知道如何解决这个问题吗? i
我有一个 UISearchBar添加到 UITableView 的顶部. // init search bar self.resultSearchController = ({
在 iOS 5 上使用带有 showCancelButton=YES 的 UISearchBar。希望在键盘下拉时取消按钮保持启用状态。使用以下代码似乎不起作用: for (id subView in
我有一个 UITableView,它的数据源和委托(delegate)不是由放置它的 View Controller 处理的,而是由另一个名为 AbstractInviteFriendsDataSou
我研究过这个问题。看起来这是一个常见问题,但我尝试过的任何方法都没有奏效。我对 iPhone 应用程序开发和 objective-c 非常陌生。此时我想做的就是在搜索栏字段中输入一个字符串,并在点击键
我有一个使用 UISearchBar 的搜索功能,该功能是即时发生的,所以我认为用“完成”替换键盘上的“搜索”按钮会更明显。 有办法做到这一点吗? 谢谢 最佳答案 您可以更改 UISearchBar
有没有人设法为 UISearchBar 的范围栏部分着色,它有一个 tintColor 属性,但设置它不会影响附加的范围栏 UISegmentedControl。我在酒吧上有一个 Handlebars
我是一名优秀的程序员,十分优秀!