使UItextfield像UISearchBar一样运行 [英] make UItextfield to function like UISearchBar

查看:111
本文介绍了使UItextfield像UISearchBar一样运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想改变 UISearchBar 的外观:所以,

I want to change the looks of UISearchBar: So,

如果有办法,我可以使我的 UITextField (在自定义搜索背景中)的功能类似于 UISearchBar ?或继承和覆盖 - (void)layoutSubviews 是唯一的方法吗?

If there's a way by which I can make my UITextField(in a custom background for search) to function like UISearchBar ? or subclassing and overriding the - (void)layoutSubviews is the only way ?

请告诉如何将其子类化!!!

Kindly tell How to subclass it !!!

推荐答案

你可以使用此流号代码更改 UISearchBar 背景。

you can change the UISearchBar background using this bellow code..

for (UIView *subview in searchBar.subviews) {
        if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
            UIView *bg = [[UIView alloc] initWithFrame:subview.frame];
            bg.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"SkyBackground.jpeg"]];
            [searchBar insertSubview:bg aboveSubview:subview];
            [subview removeFromSuperview];
            break;
        }
    }

从此代码中您可以设置图片或其他任何内容 UISearchBar 的背景。

From this code you can set image or anything else for background of UISearchBar.

另请参阅我的回答以及其他一些功能来更改搜索栏组件布局.. < a href =https://stackoverflow.com/questions/13817330/how-to-change-inside-background-color-of-uisearchbar-component-on-ios/13817915#13817915>如何更改内部背景颜色iOS上的UISearchBar组件

Also see my this answer with some other functionality to change the searchbar component layout..How to change inside background color of UISearchBar component on iOS

更新:

更改取消按钮外观使用此波纹管代码...

for change the Cancel button appearance use this bellow code...

   UIButton *cancelButton = nil;

    for(UIView *subView in searchBar.subviews)
    {
        if([subView isKindOfClass:[UIButton class]])
        {
            cancelButton = (UIButton*)subView;
            //do something here with this cancel Button
        }
    }

这篇关于使UItextfield像UISearchBar一样运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆