如何更改 UISearchBar 中的 UITextField 位置?

发布于 2024-11-25 16:24:01 字数 84 浏览 1 评论 0原文

有没有办法在 UISearchBar 中重新定位 UITextField ? 我想将 UISearchBar 内的 UITextField 向上移动一点。

Is there a way to reposition UITextField inside UISearchBar?
I'd like to move UITextField inside UISearchBar up a little bit.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

错々过的事 2024-12-02 16:24:01

UITextField 方法不再起作用,可能是由于 UISearchBar 对象的重新设计。 UISearchBar 在屏幕上显示时会调整其 UITextField 的大小,这会使 UITextField 上的所有操作无效。

但是,我发现 UISearchBar 响应 setContentInset: 方法(在 iOS 5.0.1 上测试)。它调整距封闭视图的插入距离。

这导致了涉及 NSInitation 的复杂方法:

if([aSearchBar respondsToSelector:@selector(setContentInset:)])
{
    SEL aSelector = NSSelectorFromString(@"setContentInset:");
    NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[aSearchBar methodSignatureForSelector:aSelector]];

    [inv setSelector:aSelector];
    [inv setTarget:aSearchBar];
    UIEdgeInsets anInsets = UIEdgeInsetsMake(5, 0, 5, 35);
    [inv setArgument:&anInsets atIndex:2]; //arguments 0 and 1 are self and _cmd respectively, automatically set by NSInvocation
    [inv invoke];
}

上面的代码使用 anInsets 参数中指示的 CGRect 调整 UISearchBar(实际上是 UITextField)的 contentInset 的大小。父 if 语句 (respondsToSelector:) 可以避免新版本 iOS 中此行为的变化。

针对 Swift3 和 iOS10 进行了更新:

if searchBar.responds(to: Selector("setContentInset:")) {
   let aSelector = Selector("setContentInset:")
   let anInset = UIEdgeInsetsMake(5, 0, 5, 35)
   searchBar.perform(aSelector, with: anInset)
}

The UITextField approach is not working anymore, probably due to a restyling of the UISearchBar object. UISearchBar resizes its UITextField when it is presented on screen, this invalidates all manipulations on the UITextField.

However, I discovered that UISearchBar responds to the setContentInset: method (tested on iOS 5.0.1). It adjusts the inset distance from the enclosing view.

This result in a sophisticated approach involving NSInvocation:

if([aSearchBar respondsToSelector:@selector(setContentInset:)])
{
    SEL aSelector = NSSelectorFromString(@"setContentInset:");
    NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[aSearchBar methodSignatureForSelector:aSelector]];

    [inv setSelector:aSelector];
    [inv setTarget:aSearchBar];
    UIEdgeInsets anInsets = UIEdgeInsetsMake(5, 0, 5, 35);
    [inv setArgument:&anInsets atIndex:2]; //arguments 0 and 1 are self and _cmd respectively, automatically set by NSInvocation
    [inv invoke];
}

The above code resizes the contentInset of the UISearchBar (effectively UITextField) using the CGRect indicated in the anInsets parameter. The parent if-statement (respondsToSelector:) saves yourself from changes of this behaviour in new versions of iOS.

Updated for Swift3 and iOS10:

if searchBar.responds(to: Selector("setContentInset:")) {
   let aSelector = Selector("setContentInset:")
   let anInset = UIEdgeInsetsMake(5, 0, 5, 35)
   searchBar.perform(aSelector, with: anInset)
}
a√萤火虫的光℡ 2024-12-02 16:24:01

Swift 4+

你可以试试这个。

searchBar.searchTextPositionAdjustment = UIOffset(horizontal: 5, vertical: 0)

它不会移动文本字段,但会移动文本。这对于大多数人来说应该足够了。

Swift 4+

You can try this.

searchBar.searchTextPositionAdjustment = UIOffset(horizontal: 5, vertical: 0)

It doesn't move the textField, but moves the text. This should be enough for most people.

雄赳赳气昂昂 2024-12-02 16:24:01

最好的解决方案是更改 orientationLayoutMargins

searchBar.directionalLayoutMargins = NSDirectionalEdgeInsets(top: 0.0, leading: 20.0, bottom: 0.0, trailing: 20.0)

The best solution is changing of directionalLayoutMargins

searchBar.directionalLayoutMargins = NSDirectionalEdgeInsets(top: 0.0, leading: 20.0, bottom: 0.0, trailing: 20.0)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文