猫猫学iOS 之微博项目实战(5)微博自定义搜索框searchBar

猫猫分享,必须精品

原创文章,欢迎转载。转载请注明:翟乃玉的博客
地址:http://blog.csdn.net/u013357243

一:效果

用UITextField简单定义一个搜索框
这里写图片描述

二:调用:

调用的代码,很简单,直接init就可以,以后加功能自己添加就行了。

- (void)viewDidLoad {
    [super viewDidLoad];

    // 创建搜索框
    NYSearchBar *searchBar = [[NYSearchBar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 35)];
    searchBar.placeholder = @"猫猫搜索";


    // 设置titleView为搜索框
    self.navigationItem.titleView = searchBar;
}

三:代码:

NYSearchBar.m文件内容
NYSearchBar.h文件里面没有东西,
思路很简单,就是左边放一个图片而已,可以自己添加其他东东。

//
//  NYSearchBar.m
//  猫猫微博
//
//  Created by apple on 15-7-29.
//  Copyright (c) 2015年 znycat. All rights reserved.
//

#import "NYSearchBar.h"

@implementation NYSearchBar


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {

        self.font = [UIFont systemFontOfSize:13];

        self.background = [UIImage imageWithStretchableName:@"searchbar_textfield_background"];

        // 设置左边的view
        // initWithImage:默认UIImageView的尺寸跟图片一样
        UIImageView *imageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"searchbar_textfield_search_icon"]];

        // 为了空出左边一小块设置的
        imageV.width += 10;
        imageV.contentMode = UIViewContentModeCenter;
        self.leftView = imageV;
        // 一定要设置,想要显示搜索框左边的视图,一定要设置左边视图的模式
        self.leftViewMode = UITextFieldViewModeAlways;

    }
    return self;
}



@end

推荐一个iOS学习帅气的网站 : code4app

各种各样的iOS效果和源码都用,随下随用。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值