关于自动scrollView自动布局(商城的搜索界面)

界面如下

C9682973-7E73-4676-81D6-95B80C0FE470.png

截得的图不太好动态图也不太好凑合看吧,首先分析一下这个界面,怎嘛实现
第一 可以用collectionView来实现,用collectionView来实现重点在collectionView的布局类..
第二 可以自己用scrollView上加button(前提button不是太多如果太多就用第一种方法实现 因为collectionView 的cell可以重用)
我是用第二种方法写的......先分析一下,在说明我的思路
肯定要先写一个scrollView了在再scrollView上加上一个contentView. contentView的大小由里面的一堆按钮个决定(是不用一开始就确定scrollView的contentOfSize的他是被里面的按钮撑开的)这点是重点可以看一下串哥的博客http://adad184.com/2015/12/01/scrollview-under-autolayout/

里面按钮的布局思路就是先把第一个按钮布局好 再布局第二个的时候判断 后面的宽度是否能够放得下,能放得下就布局在第一个按钮后面,不能放的下就布局在上一个按钮下面...依次循环下去
下面也是重要的一步就是判断最后一个按钮(记得要写上最后一个按钮距离他的父视图下面的布局约束 这样就把父视图contentView撑开了, contentView也会自动撑开scrollView).

下面就是代码具体实现
直接复制就好

//
//  ViewController.m
//  搜索界面
//
//  Created by 3D on 16/9/4.
//  Copyright © 2016年 3D. All rights reserved.
//
#define BT_HEIGHT 31
#define GAP_WIDTH 10
//获取屏幕 宽度、高度
#define SCREEN_WIDTH ([UIScreen   mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
#import "ViewController.h"
#import "Masonry.h"
@interface ViewController ()

@property (nonatomic, strong) NSArray* historyArr;
@property (nonatomic, strong) NSArray* storeArr;
@property (nonatomic, strong) NSArray* hotArr;
@property (nonatomic, strong) UIView *contentView;

@end

@implementation ViewController
{
CGFloat allBt_W;
UIButton *lastBT;
UILabel *nearLB;//最近搜索
UILabel *storeLB;//店铺搜索
}

- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
allBt_W = 0;

//    [self.navigationController.navigationBar addSubview:searchTextField];
//    searchTextField.frame = CGRectMake(43, 7, SCREEN_WIDTH*269/375, 30);
//    searchTextField.backgroundColor = [UIColor whiteColor];
//    NSString *placeholderString = @"查找宠物商品";
//    NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc]initWithString:placeholderString];
//    [placeholder addAttribute:NSFontAttributeName value:[[KDFont new] getF30Font] range:NSMakeRange(0, placeholderString.length)];
//    [placeholder addAttribute:NSForegroundColorAttributeName value:[KDColor getC3Color] range:NSMakeRange(0,placeholderString.length)];
//    searchTextField.tintColor = [KDColor getC3Color];
//    searchTextField.attributedPlaceholder = placeholder;
//    searchTextField.layer.cornerRadius = 5;
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
[self.view addSubview:scrollView];
self.contentView = [UIView new];
[scrollView addSubview:self.contentView];
[_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.mas_equalTo(0);
    make.width.mas_equalTo(SCREEN_WIDTH);
}];



self.historyArr = @[@"小泽玛利亚",@"苍井空",@"武藤兰",@"栋神",@"小泽玛利亚",@"苍井空",@"武藤兰",@"小泽玛利亚",@"苍井空",@"小泽玛利亚",@"苍井空",@"武藤兰",@"栋神",@"小泽玛利亚",@"苍井空",@"武藤兰",@"小泽玛利亚",@"苍井空"];
self.storeArr = @[@"武藤兰的情趣用品店",@"栋神的情趣用品店",@"小泽玛利亚的情趣用品店",@"苍井空的情趣用品店",@"武藤兰的情趣用品店",@"栋神的情趣用品店"];
//    _historyArr =  nil;
self.hotArr = @[@"狗粮",@"女朋友",@"狗粮",@"我要媳妇😭",@"虐狗",@"😭",@"虐狗",@"😭",@"狗粮",@"女朋友",@"虐狗",@"😭",@"狗粮",@"女朋友",@"狗粮",@"我要媳妇😭",@"虐狗",@"😭",@"虐狗",@"😭",@"狗粮",@"女朋友",@"虐狗",@"😭"];
//在调用下面三个方法的时候 要保证已经走了网络(三个数组经过网络请求初始化了)
[self creatHistoryUI:_historyArr];
[self creatStoreUI:_storeArr];
[self creatHotUI:_hotArr];

}

-(void)creatHistoryUI:(NSArray*)arr{
UILabel *nearSearchLB = [UILabel new];
nearLB = nearSearchLB;
nearSearchLB.font = [UIFont systemFontOfSize:14];
nearSearchLB.textColor = [UIColor grayColor];
nearSearchLB.text = @"最近搜索";
[self.contentView addSubview:nearSearchLB];
[nearSearchLB mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.mas_equalTo(16);
    make.top.mas_equalTo(64+20);
}];
UIImageView *imageView = [UIImageView new];
[self.contentView addSubview:imageView];
imageView.image = [UIImage imageNamed:@"icon_shanchu"];
[imageView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.right.mas_equalTo(-16);
    make.top.mas_equalTo(64+20);
    make.width.height.mas_equalTo(14);
}];
if (arr==nil || arr.count == 0) {
    UILabel *noHistoryLB = [UILabel new];
    noHistoryLB.font = [UIFont systemFontOfSize:14];
    noHistoryLB.textColor = [UIColor grayColor];
    noHistoryLB.text = @"无历史搜索";
    [self.contentView addSubview:noHistoryLB];
    [noHistoryLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(imageView.mas_bottom).mas_equalTo(15);
    }];
}else{
    lastBT = nil;
    for (int i = 0; i<arr.count; i++) {
        UIButton *historyBT = [UIButton new];
        [historyBT addTarget:self action:@selector(butionClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:historyBT];
        historyBT.layer.cornerRadius = 2;
        historyBT.layer.borderColor = [UIColor grayColor].CGColor;
        historyBT.layer.borderWidth = 1;
        [historyBT setTitle:arr[i] forState:0];
        [historyBT setTitleColor:[UIColor grayColor] forState:0];
        historyBT.titleLabel.font = [UIFont systemFontOfSize:14];
        CGFloat historyBT_W = [self butionWidth:arr[i]];
        allBt_W = allBt_W+(historyBT_W+GAP_WIDTH);
        if (i==0) {
            [historyBT mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(nearSearchLB.mas_bottom).mas_equalTo(15);
                make.left.mas_equalTo(16);
                make.height.mas_equalTo(BT_HEIGHT);
                make.width.mas_equalTo(historyBT_W);
            }];
            lastBT = historyBT;
        }else{
            [historyBT mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(historyBT_W);
                make.height.mas_equalTo(BT_HEIGHT);
                if (SCREEN_WIDTH-allBt_W-16-6 >= 0) {//不换行
                    make.left.mas_equalTo(lastBT.mas_right).mas_equalTo(GAP_WIDTH);
                    make.top.mas_equalTo(lastBT);
                }else{
                    allBt_W = historyBT_W+GAP_WIDTH;
                    make.left.mas_equalTo(16);
                    make.top.mas_equalTo(lastBT.mas_bottom).mas_equalTo(GAP_WIDTH);
                }
            }];
            lastBT = historyBT;
        }
    }
}
}

-(void)creatStoreUI:(NSArray*)arr{
allBt_W = 0;
UILabel *shopSearchLB = [UILabel new];
storeLB = shopSearchLB;
shopSearchLB.font = [UIFont systemFontOfSize:14];
shopSearchLB.textColor = [UIColor grayColor];
shopSearchLB.text = @"店铺搜索";
[self.contentView addSubview:shopSearchLB];
[shopSearchLB mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.mas_equalTo(16);
    if (self.historyArr.count == 0 || self.historyArr == nil ) {
        make.top.mas_equalTo(nearLB.mas_bottom).mas_equalTo(66);
    }else{
        make.top.mas_equalTo(lastBT.mas_bottom).mas_equalTo(20);
    }
}];
if (arr==nil || arr.count == 0) {
    UILabel *noHistoryLB = [UILabel new];
    noHistoryLB.font = [UIFont systemFontOfSize:14];
    noHistoryLB.textColor = [UIColor orangeColor];
    noHistoryLB.text = @"no店铺搜索";
    [self.contentView addSubview:noHistoryLB];
    [noHistoryLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(shopSearchLB.mas_bottom).mas_equalTo(15);
    }];
}else{
    lastBT = nil;
    for (int i = 0; i<arr.count; i++) {
        UIButton *shopBT = [UIButton new];
        [shopBT addTarget:self action:@selector(butionClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:shopBT];
        shopBT.layer.cornerRadius = 2;
        shopBT.layer.borderColor = [UIColor grayColor].CGColor;
        shopBT.layer.borderWidth = 1;
        [shopBT setTitle:arr[i] forState:0];
        [shopBT setTitleColor:[UIColor grayColor] forState:0];
        shopBT.titleLabel.font = [UIFont systemFontOfSize:14];
        CGFloat shopBT_W = [self butionWidth:arr[i]];
        allBt_W = allBt_W+(shopBT_W+GAP_WIDTH);
        if (i==0) {
            [shopBT mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(shopSearchLB.mas_bottom).mas_equalTo(15);
                make.left.mas_equalTo(16);
                make.height.mas_equalTo(BT_HEIGHT);
                make.width.mas_equalTo(shopBT_W);
            }];
            lastBT = shopBT;
        }else{
            [shopBT mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(shopBT_W);
                make.height.mas_equalTo(BT_HEIGHT);
                if (SCREEN_WIDTH-allBt_W-16-6 >= 0) {//不换行
                    make.left.mas_equalTo(lastBT.mas_right).mas_equalTo(GAP_WIDTH);
                    make.top.mas_equalTo(lastBT);
                }else{
                    allBt_W = shopBT_W+GAP_WIDTH;
                    make.left.mas_equalTo(16);
                    make.top.mas_equalTo(lastBT.mas_bottom).mas_equalTo(GAP_WIDTH);
                }
            }];
            lastBT = shopBT;
        }
    }
}

}

-(void)creatHotUI:(NSArray*)arr{
allBt_W = 0;
UILabel *hotSearchLB = [UILabel new];
hotSearchLB.font = [UIFont systemFontOfSize:14];
hotSearchLB.textColor = [UIColor grayColor];
hotSearchLB.text = @"热门搜索";
[self.contentView addSubview:hotSearchLB];
[hotSearchLB mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.mas_equalTo(16);
    if (self.storeArr.count == 0 || self.storeArr == nil ) {
        make.top.mas_equalTo(storeLB.mas_bottom).mas_equalTo(66);
    }else{
        make.top.mas_equalTo(lastBT.mas_bottom).mas_equalTo(20);
    }
}];
if (arr==nil || arr.count == 0) {
    UILabel *noHotLB = [UILabel new];
    noHotLB.font = [UIFont systemFontOfSize:14];
    noHotLB.textColor = [UIColor grayColor];
    noHotLB.text = @"no热门搜索";
    [self.contentView addSubview:noHotLB];
    [noHotLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(hotSearchLB.mas_bottom).mas_equalTo(15);
        make.bottom.mas_equalTo(-44);
    }];
}else{
    lastBT = nil;
    for (int i = 0; i<arr.count; i++) {
        UIButton *hotBT = [UIButton new];
        [hotBT addTarget:self action:@selector(butionClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:hotBT];
        hotBT.layer.cornerRadius = 2;
        hotBT.layer.borderColor = [UIColor grayColor].CGColor;
        hotBT.layer.borderWidth = 1;
        [hotBT setTitle:arr[i] forState:0];
        [hotBT setTitleColor:[UIColor grayColor] forState:0];
        hotBT.titleLabel.font = [UIFont systemFontOfSize:14];
        CGFloat hotBT_W = [self butionWidth:arr[i]];
        allBt_W = allBt_W+(hotBT_W+GAP_WIDTH);
        if (i==0) {
            [hotBT mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(hotSearchLB.mas_bottom).mas_equalTo(15);
                make.left.mas_equalTo(16);
                make.height.mas_equalTo(BT_HEIGHT);
                make.width.mas_equalTo(hotBT_W);
            }];
            lastBT = hotBT;
        }else{
            if (i==arr.count-1) {
                [hotBT mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.width.mas_equalTo(hotBT_W);
                    make.height.mas_equalTo(BT_HEIGHT);
                    if (SCREEN_WIDTH-allBt_W-16-6 >= 0) {//不换行
                        make.left.mas_equalTo(lastBT.mas_right).mas_equalTo(GAP_WIDTH);
                        make.top.mas_equalTo(lastBT);
                    }else{
                        allBt_W = hotBT_W+GAP_WIDTH;
                        make.left.mas_equalTo(16);
                        make.top.mas_equalTo(lastBT.mas_bottom).mas_equalTo(GAP_WIDTH);
                    }
                    make.bottom.mas_equalTo(-10);
                }];
                lastBT = nil;
            }else{
                [hotBT mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.width.mas_equalTo(hotBT_W);
                    make.height.mas_equalTo(BT_HEIGHT);
                    if (SCREEN_WIDTH-allBt_W-16-6 >= 0) {//不换行
                        make.left.mas_equalTo(lastBT.mas_right).mas_equalTo(GAP_WIDTH);
                        make.top.mas_equalTo(lastBT);
                    }else{
                        allBt_W = hotBT_W;
                        make.left.mas_equalTo(16);
                        make.top.mas_equalTo(lastBT.mas_bottom).mas_equalTo(GAP_WIDTH);
                    }
                }];
                lastBT = hotBT;
            }
        }
    }
}

}

-(void)butionClick:(UIButton*)sender{

NSLog(@"semder.title = %@",sender.titleLabel.text);
}

-(CGFloat)butionWidth:(NSString *)title{

NSMutableDictionary *attributes = [NSMutableDictionary dictionary];    attributes[NSFontAttributeName] = [UIFont systemFontOfSize:14];
CGRect rect = [title boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 13) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attributes context:nil];
CGFloat butionW = rect.size.width+40;
return butionW;
}


- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
2016-09-05 00_29_58.gif
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,014评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,796评论 3 386
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,484评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,830评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,946评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,114评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,182评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,927评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,369评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,678评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,832评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,533评论 4 335
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,166评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,885评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,128评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,659评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,738评论 2 351

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,067评论 4 62
  • 概率在我们的日常生活中无处不在,然而很多人可能没有注意到它,于是常常犯了一些概率错误而不自知。其中有三种错误最为常...
    余先飞阅读 2,480评论 0 0
  • 产假结束了,五个半月的时间我从一个妈妈的孩子蜕变成了孩子的妈妈,随着女儿的一天天长大,我从一开始的烦闷、后悔,逐...
    乐支君子阅读 217评论 0 0
  • 小的时候,喜欢握紧妈妈的手。在陌生的世界里,妈妈的手是那么温柔。饿的时候,无论妈妈多忙,看着饥饿的我,妈妈就会放下...
    顺着风飞翔阅读 281评论 1 3
  • 董沛沛 洛阳 焦点讲师班三期 坚持原创分享第177天 舒尔特方格是世界上最专业、最普及、最简单的专注力训练法,不仅...
    缘源流长阅读 242评论 0 0