Right align magnifying glass icon in UISearchBar(UISearchBar中的右对齐放大镜图标)
问题描述
在 Cocoa-Touch 中,我们找到了一个控件 UISearchBar
.我需要对其进行自定义,以便文本字段中出现的搜索图标(我们单击它执行相应的操作)右对齐.通常我们会发现它左对齐.有可能这样做吗?我做过R&D 但是找不到...
我该怎么做?有什么好的教程可以找到吗?
不幸的是,UISearchBar
并不是为了直接支持这一点而设计的.在完成我们想要的事情时,我们可能不得不使用在视觉或功能方面妥协或编写大量自定义代码的变通方法.
我已经概述了一些接近我们想要并且可能有用的方法.
访问子视图方法
UISearchBar
中的文本是一个 UITextField
,它是该 UISearchBar
的子视图,可以通过通常的方式访问,即 view.subviews
.
放大镜搜索图标是 属性用于其他内容,例如搜索结果按钮和书签按钮.将搜索图标放在此处可能会在某种程度上与此功能发生冲突,即使您能够使用偏移填充来适当定位它.
至少您可以使用这种方法将图标提取为 UIImageView
并将其显式放置在您认为合适的位置,就像任何其他视图一样,并编写自定义代码来处理点击事件等.
自定义外观方法
在 iOS v5.0 及更高版本中,您可以使用列出的方法自定义搜索栏的外观 这里.以下代码使用偏移量在 UISearchBar
中定位图标和文本:
[self.searchBar setPositionAdjustment:UIOffsetMake(255, 0) forSearchBarIcon:UISearchBarIconSearch];[self.searchBar setSearchTextPositionAdjustment:UIOffsetMake(-270, 0)];
在宽度为 320 的标准 UISearchBar
上,它如下所示:
这将使与图标相关联的所有事件功能按预期工作,但不幸的是 UITextField
很混乱,尽管它很宽,但它只显示其文本的一小部分.如果你能找到一种方法让文本显示超出它认为是 UITextField
的结尾,这可能是你最好的选择.
In Cocoa-Touch we find a control UISearchBar
. I need to customize it so that the search icon (which we click performs the respective action) present in the textfield is right aligned. Normally we find it left aligned. Is it possible to do so? I have done R & D but couldn't find it ...
How can I do it? Are there any good tutorials where we can find it?
Unfortunately the UISearchBar
wasn't designed to directly support this. In accomplishing what we want it's likely that we're going to have to use workarounds that compromise visually or functionality wise or write a lot of customising code.
I've outlined a couple of approaches that get close to what we want and may be of use.
Accessing Subviews Approach
The text within a UISearchBar
is a UITextField
that is a subview of that UISearchBar
and is accessible through the usually means i.e. view.subviews
.
The magnifying glass search icon is a UIImageView
in the leftView
property of the UITextField
. We can switch it over to the right using the following code:
// The text within a UISearchView is a UITextField that is a subview of that UISearchView.
UITextField *searchField;
for (UIView *subview in self.searchBar.subviews)
{
if ([subview isKindOfClass:[UITextField class]]) {
searchField = (UITextField *)subview;
break;
}
}
// The icon is accessible through the 'leftView' property of the UITextField.
// We set it to the 'rightView' instead.
if (searchField)
{
UIView *searchIcon = searchField.leftView;
if ([searchIcon isKindOfClass:[UIImageView class]]) {
NSLog(@"aye");
}
searchField.rightView = searchIcon;
searchField.leftViewMode = UITextFieldViewModeNever;
searchField.rightViewMode = UITextFieldViewModeAlways;
}
Which gives us the following result:
As you can see the icon overruns the edge of the rounded rectangle and the clear icon has disappeared. In addition, the rightView
property was intended for other content such as the search results button and bookmarks button. Putting the search icon here may conflict with this functionality in some way even if you were able to use padding of offsets to position it appropriately.
In the very least you can use this approach to extract the icon as a UIImageView
and position it explicitly where you see fit as you would any other view and write custom code to handle tap events etc.
Customizing Appearance Approach
In iOS v5.0 and later, you can customize the appearance of search bars using the methods listed here. The following code makes use of offsets to position the icon and text within the UISearchBar
:
[self.searchBar setPositionAdjustment:UIOffsetMake(255, 0) forSearchBarIcon:UISearchBarIconSearch];
[self.searchBar setSearchTextPositionAdjustment:UIOffsetMake(-270, 0)];
On a standard UISearchBar
of 320 width it looks like the following:
This would keep all event functionality associated with the icon working as expected but unfortunately the UITextField
is confused and despite its width, it only displays a very small portion of its text. If you could find a way to allow the text to display beyond what it believes to be the end of the UITextField
, this will probably be your best bet.
这篇关于UISearchBar中的右对齐放大镜图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UISearchBar中的右对齐放大镜图标
基础教程推荐
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01