iOS自动布局——Masonry详解

2023-11-20

欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~

本文由鹅厂新鲜事儿发表于云+社区专栏

作者:oceanlong | 腾讯 移动客户端开发工程师

前言

UI布局是整个前端体系里不可或缺的一环。代码的布局是设计语言与用户视觉感受沟通的桥梁,不论它看起来多么简单或是琐碎,但不得不承认,绝大部分软件开发的问题,都是界面问题。那么,如何高效的完成UI开发,也是软件行业一直在克服的问题。

img

所以,软件界面开发的核心点即是:如何减少UI设计稿的建模难度和减少建模转化到代码的实现难度

最初iOS提供了平面直角坐标系的方式,来解决布局问题,即所谓的手动布局。平面直角坐标系确实是一套完备在理论,这在数学上已经验证过了,只要我们的屏幕还是平面,它就肯定是有效的。但有效不一定高效,我们在日常的生活中,很少会用平面直角坐标系来向人描述位置关系。更多的是依靠相对位置。

所幸,iOS为我们提供自动布局的方法,来解决这一困境。

img

自动布局的基本理念

其实说到本质,它和手动布局是一样的。对一个控件放在哪里,我们依然只关心它的(x, y, width, height)。但手动布局的方式是,一次性计算出这四个值,然后设置进去,完成布局。但当父控件或屏幕发生变化时,子控件的计算就要重新来过,非常麻烦。

因此,在自动布局中,我们不再关心(x, y, width, height)的具体值,我们只关心(x, y, width, height)四个量对应的约束。

约束

那么何为约束呢?

obj1.property1 =(obj2.property2 * multiplier)+ constant value

子控件的某一个量一定与另一个控件的某一个量呈线性关系,这就是约束。

那么,给(x, y, width, height)四个量,分别给一个约束,就可以确定一个控件的最终位置。

    //创建左边约束
    NSLayoutConstraint *leftLc = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:20];
    [self.view addConstraint:leftLc];

这一段代码即是:控件(blueView)的 x = rootView的x * 1.0 + 20这里一定要注意,这样的一条约束,涉及了子控件和父控件,所以这条约束一定要添加到父控件中。

添加约束的规则:

  • 如果两个控件是父子控件,则添加到父控件中。
  • 如果两个控件不是父子控件,则添加到层级最近的共同父控件中。

示例

    //关闭Autoresizing
    blueView.translatesAutoresizingMaskIntoConstraints = NO;
    
    //创建左边约束
    NSLayoutConstraint *leftLc = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:20];
    [self.view addConstraint:leftLc];
    
    //创建右边约束
    NSLayoutConstraint *rightLc = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:-20];
    [self.view addConstraint:rightLc];
    
    //创建底部约束
    NSLayoutConstraint *bottomLc = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-20];
    [self.view addConstraint:bottomLc];
    
    //创建高度约束
    NSLayoutConstraint *heightLc = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:50];
    [blueView addConstraint: heightLc];

img

我们注意到,自动布局其实工作分两步:

  1. 创建视图的约束
  2. 将约束添加到合适的位置约束关系从上面的描述中,已经非常清晰了。那么如何寻找约束添加的合适位置呢?

img


到这里,我们只是解决了如何减少UI设计稿的建模难度的问题,显然,减少建模转化到代码的实现难度这个效果没能达成。关于如何解决减少建模转化到代码的实现难度的问题,

开源库

上面的代码,我们可以看到,虽然自动布局已经比手动布局优雅不少了,但它依然行数较多。每条约束大约都需要三行代码,面对复杂的页面,这样开发出来,会很难阅读。

Masonry则为我们解决了这个问题。

Masonry地址

引入Masonry

我们选择使用Cocoapods的方式。引入比较简单:

  1. 我们先在工程目录下,创建Podfile文件:

img

2.编辑Podfile

img

其中,'IosOcDemo’就是我们工程的名字,根据需要,我们自行替换。

3.添加依赖

完成后,执行指令pod install。CocoaPods就会为我们自动下载并添加依赖。

实践

img

这样的一个代码,用手动布局,我们大致的代码应该是这样:

-(void)initBottomView
{
    self.bottomBarView = [[UIView alloc]initWithFrame:CGRectZero];
    self.bottomButtons = [[NSMutableArray alloc]init];
    _bottomBarView.backgroundColor = [UIColor yellowColor];
    [self addSubview:_bottomBarView];
    for(int i = 0 ; i < 3 ; i++)
    {
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectZero];
        button.backgroundColor = [UIColor redColor];
        [_bottomButtons addObject:button];
       [self addSubview:button];
    }
}


-(void)layoutBottomView
{
    _bottomBarView.frame = CGRectMake(20, _viewHeight - 200, _viewWidth - 40, 200);
    for (int i = 0 ; i < 3; i++) {
        UIButton *button = _bottomButtons[i];
        CGFloat x = i * (_viewWidth - 40 - 20 * 4) / 3 + 20*(i+1) + 20;
        CGFloat y = _viewHeight - 200;
        CGFloat width = (_viewWidth - 40 - 20 * 4) / 3;
        CGFloat height = 200;
        button.frame = CGRectMake(x, y, width, height);
        
    }
}

我们来看一下,在Masonry的帮助下,我们可以把刚刚的代码写成什么样的:

   -(void)initBottomView
{
        _bottomBarView = [[UIView alloc]initWithFrame:CGRectZero];
        _bottomBarView.backgroundColor = [UIColor yellowColor];
        _bottomBarView.translatesAutoresizingMaskIntoConstraints = NO;
        [self addSubview:_bottomBarView];
        [_bottomBarView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(20);
            make.right.equalTo(self).with.offset(-20);
            make.height.mas_equalTo(200);
            make.bottom.equalTo(self);
        }];
    
    _bottomButtons = [[NSMutableArray alloc]init];
    for(int i = 0 ; i < 3 ; i++)
    {
        UIButton *button = [[UIButton alloc]initWithFrame: CGRectZero];
        button.backgroundColor = [UIColor redColor];
        button.translatesAutoresizingMaskIntoConstraints = NO;
        [_bottomButtons addObject:button];
        [_bottomBarView addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            if (i == 0) {
                make.left.mas_equalTo(20);
            }else{
                UIButton *previousButton = _bottomButtons[i-1];
                make.left.equalTo(previousButton.mas_right).with.offset(20);
            }
            make.top.mas_equalTo(_bottomBarView.mas_top);
            make.width.equalTo(_bottomBarView.mas_width).with.multipliedBy(1.0f/3).offset(-20*4/3);
            make.height.equalTo(_bottomBarView.mas_height);
        }];
        
    }
}

我们可以看到在Masonry的封装下,代码变得非常简练易读,需要行数略有增加,但是计算过程减少了,我们能更加关注于多个UIView间的位置关系,这与当前的UI设计语言是契合的。所以Masonry能否让我们更直观地表达UI。

源码解读

Masonry的封装很有魅力,那么,我们可以简单地来看一下,它是如何封装的。我们再仔细看一下Masonry的API会发现,我们是直接在UIView上进行调用的。也就是说,Masonry对UIView进行了扩展。

View+MASUtilities.h中:

#if TARGET_OS_IPHONE || TARGET_OS_TV

    #import <UIKit/UIKit.h>
    #define MAS_VIEW UIView
    #define MAS_VIEW_CONTROLLER UIViewController
    #define MASEdgeInsets UIEdgeInsets

然后在View+MASAdditions.h中,我们看到了Masonry的扩展:

#import "MASUtilities.h"
#import "MASConstraintMaker.h"
#import "MASViewAttribute.h"

/**
 *  Provides constraint maker block
 *  and convience methods for creating MASViewAttribute which are view + NSLayoutAttribute pairs
 */
@interface MAS_VIEW (MASAdditions)

/**
 *  following properties return a new MASViewAttribute with current view and appropriate NSLayoutAttribute
 */
@property (nonatomic, strong, readonly) MASViewAttribute *mas_left;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_top;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_right;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_bottom;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_leading;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_trailing;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_width;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_height;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_centerX;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_centerY;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_baseline;
@property (nonatomic, strong, readonly) MASViewAttribute *(^mas_attribute)(NSLayoutAttribute attr);

...

/**
 *  Creates a MASConstraintMaker with the callee view.
 *  Any constraints defined are added to the view or the appropriate superview once the block has finished executing
 *
 *  @param block scope within which you can build up the constraints which you wish to apply to the view.
 *
 *  @return Array of created MASConstraints
 */
- (NSArray *)mas_makeConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *make))block;

一些,适配的代码,我省略了,先看核心代码。在刚刚的例子中,我们正是调用的mas_makeConstraints方法。

- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block {
    self.translatesAutoresizingMaskIntoConstraints = NO;
    MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self];
    block(constraintMaker);
    return [constraintMaker install];
}

mas_makeConstraints方法比较简单,只是封装了MASConstraintMaker初始化,设置约束和安装。这里的block就是我们刚刚在外层设置的约束的函数指针。也就是这一串:

^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(10);
        make.right.equalTo(self.view).with.offset(-10);
        make.height.mas_equalTo(50);
        make.bottom.equalTo(self.view).with.offset(-10);
    }

由于约束条件的设置比较复杂,我们先来看看初始化和安装。

初始化
- (id)initWithView:(MAS_VIEW *)view {
    self = [super init];
    if (!self) return nil;
    
    self.view = view;
    self.constraints = NSMutableArray.new;
    
    return self;
}

初始化的代码比较简单,将传入的view放入MASConstraintMaker成员,然后创建MASConstraintMaker的约束容器(NSMutableArray)。

安装
- (NSArray *)install {
    if (self.removeExisting) {
        NSArray *installedConstraints = [MASViewConstraint installedConstraintsForView:self.view];
        for (MASConstraint *constraint in installedConstraints) {
            [constraint uninstall];
        }
    }
    NSArray *constraints = self.constraints.copy;
    for (MASConstraint *constraint in constraints) {
        constraint.updateExisting = self.updateExisting;
        [constraint install];
    }
    [self.constraints removeAllObjects];
    return constraints;
}

安装的代码分为三块:

  1. 判断是否需要移除已有的约束。如果需要,会遍历已有约束,然后逐个uninstall
  2. copy已有的约束,遍历,并逐一install
  3. remove掉所有约束,并将已添加的constraints返回。

install的方法,还是继续封装到了Constraint中,我们继续跟进阅读:

我们会发现Constraint只是一个接口,Masonry中对于Constraint接口有两个实现,分别是:MASViewConstraintMASCompositeConstraint。这两个类,分别是单个约束和约束集合。在上面的例子中,我们只是对单个UIView进行约束,所以我们先看MASViewConstraint的代码。以下代码MASViewConstraint进行了一定程度的简化,省略了一些扩展属性,只展示我们的例子中,会执行的代码:

- (void)install {
    if (self.hasBeenInstalled) {
        return;
    }
    ...
    
    MAS_VIEW *firstLayoutItem = self.firstViewAttribute.item;
    NSLayoutAttribute firstLayoutAttribute = self.firstViewAttribute.layoutAttribute;
    MAS_VIEW *secondLayoutItem = self.secondViewAttribute.item;
    NSLayoutAttribute secondLayoutAttribute = self.secondViewAttribute.layoutAttribute;

    // alignment attributes must have a secondViewAttribute
    // therefore we assume that is refering to superview
    // eg make.left.equalTo(@10)
    if (!self.firstViewAttribute.isSizeAttribute && !self.secondViewAttribute) {
        secondLayoutItem = self.firstViewAttribute.view.superview;
        secondLayoutAttribute = firstLayoutAttribute;
    }
    
    MASLayoutConstraint *layoutConstraint
        = [MASLayoutConstraint constraintWithItem:firstLayoutItem
                                        attribute:firstLayoutAttribute
                                        relatedBy:self.layoutRelation
                                           toItem:secondLayoutItem
                                        attribute:secondLayoutAttribute
                                       multiplier:self.layoutMultiplier
                                         constant:self.layoutConstant];
    
    layoutConstraint.priority = self.layoutPriority;
    layoutConstraint.mas_key = self.mas_key;
    
   if (self.secondViewAttribute.view) {
        MAS_VIEW *closestCommonSuperview = [self.firstViewAttribute.view mas_closestCommonSuperview:self.secondViewAttribute.view];
        NSAssert(closestCommonSuperview,
                 @"couldn't find a common superview for %@ and %@",
                 self.firstViewAttribute.view, self.secondViewAttribute.view);
        self.installedView = closestCommonSuperview;
    } else if (self.firstViewAttribute.isSizeAttribute) {
        self.installedView = self.firstViewAttribute.view;
    } else {
        self.installedView = self.firstViewAttribute.view.superview;
    }


    MASLayoutConstraint *existingConstraint = nil;
    ...
    else {
        [self.installedView addConstraint:layoutConstraint];
        self.layoutConstraint = layoutConstraint;
        [firstLayoutItem.mas_installedConstraints addObject:self];
    }
}

自动布局是一种相对布局,所以,绝大部分情况下,需要两个UIView(约束方与参照方)。在上面的方法中:

  • firstLayoutItem是约束方,secondLayoutItem是参照方
  • firstLayoutAttribute是约束方的属性,secondLayoutAttribute是参照方的属性。
  • MASLayoutConstraint就是NSLayoutConstraint的子类,只是添加了mas_key属性。到这里,我们就与系统提供的API对应上了。
    NSLayoutConstraint *leftLc = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:20];
    [self.view addConstraint:leftLc];

再看看我们之前用系统API完成的例子,是不是格外熟悉?

那么接下来,我们就是要阅读

            make.left.equalTo(self).with.offset(20);
            make.right.equalTo(self).with.offset(-20);
            make.height.mas_equalTo(200);
            make.bottom.equalTo(self);

是如何变成firstLayoutItem, secondLayoutItem, firstLayoutAttribute, secondLayoutAttributelayoutRelation的。

约束条件的设置

回到前面的:

- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block {
    self.translatesAutoresizingMaskIntoConstraints = NO;
    MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self];
    block(constraintMaker);
    return [constraintMaker install];
}

我们接下来,就要看block的实现:

block其实是一个函数指针。此处真正调用的方法是:

            make.left.equalTo(self).with.offset(20);
            make.right.equalTo(self).with.offset(-20);
            make.height.mas_equalTo(200);
            make.bottom.equalTo(self);

我们挑选其中一个,来看看源码实现:

left
- (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute {
    return [self constraint:nil addConstraintWithLayoutAttribute:layoutAttribute];
}

- (MASConstraint *)left {
    return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeft];
}

- (MASConstraint *)constraint:(MASConstraint *)constraint addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute {
    MASViewAttribute *viewAttribute = [[MASViewAttribute alloc] initWithView:self.view layoutAttribute:layoutAttribute];
    MASViewConstraint *newConstraint = [[MASViewConstraint alloc] initWithFirstViewAttribute:viewAttribute];
    if ([constraint isKindOfClass:MASViewConstraint.class]) {
        //replace with composite constraint
        NSArray *children = @[constraint, newConstraint];
        MASCompositeConstraint *compositeConstraint = [[MASCompositeConstraint alloc] initWithChildren:children];
        compositeConstraint.delegate = self;
        [self constraint:constraint shouldBeReplacedWithConstraint:compositeConstraint];
        return compositeConstraint;
    }
    if (!constraint) {
        newConstraint.delegate = self;
        [self.constraints addObject:newConstraint];
    }
    return newConstraint;
}

在对单个view添加约束时,constraint为nil。我们直接生成了一个新约束newConstraint。它的firstViewAttribute就是我们传入的NSLayoutAttributeLeft

equalTo

- (MASConstraint * (^)(id))equalTo {
    return ^id(id attribute) {
        return self.equalToWithRelation(attribute, NSLayoutRelationEqual);
    };
}

- (MASConstraint * (^)(id, NSLayoutRelation))equalToWithRelation {
    return ^id(id attribute, NSLayoutRelation relation) {
        if ([attribute isKindOfClass:NSArray.class]) {
            NSAssert(!self.hasLayoutRelation, @"Redefinition of constraint relation");
            NSMutableArray *children = NSMutableArray.new;
            for (id attr in attribute) {
                MASViewConstraint *viewConstraint = [self copy];
                viewConstraint.layoutRelation = relation;
                viewConstraint.secondViewAttribute = attr;
                [children addObject:viewConstraint];
            }
            MASCompositeConstraint *compositeConstraint = [[MASCompositeConstraint alloc] initWithChildren:children];
            compositeConstraint.delegate = self.delegate;
            [self.delegate constraint:self shouldBeReplacedWithConstraint:compositeConstraint];
            return compositeConstraint;
        } else {
            NSAssert(!self.hasLayoutRelation || self.layoutRelation == relation && [attribute isKindOfClass:NSValue.class], @"Redefinition of constraint relation");
            self.layoutRelation = relation;
            self.secondViewAttribute = attribute;
            return self;
        }
    };
}

此处,我们依然先看attribute不是NSArray的情况。这里在单个属性的约束中,就比较简单了,将relationattribue传入MASConstraint对应的成员。

在上面介绍install方法时,我们就曾提到过:

  MASLayoutConstraint *layoutConstraint
        = [MASLayoutConstraint constraintWithItem:firstLayoutItem
                                        attribute:firstLayoutAttribute
                                        relatedBy:self.layoutRelation
                                           toItem:secondLayoutItem
                                        attribute:secondLayoutAttribute
                                       multiplier:self.layoutMultiplier
                                         constant:self.layoutConstant];

firstLayoutItemsecondLayoutIteminstall方法中已收集完成,此时,经过leftequalTo我们又收集到了:firstViewAttributesecondViewAttributelayoutRelation胜利即在眼前。

- (MASConstraint * (^)(CGFloat))offset {
    return ^id(CGFloat offset){
        self.offset = offset;
        return self;
    };
}

- (void)setOffset:(CGFloat)offset {
    self.layoutConstant = offset;
}

通过OC的set语法,Masonryoffset传入layoutConstant。

至此,layoutConstraint就完成了全部的元素收集,可以使用添加约束的方式,只需要解决最后一个问题,约束添加到哪里呢?我们似乎在调用时,并不需要关心这件事情,那说明框架帮我们完成了这个工作。

closestCommonSuperview

我们在MASViewConstraint中,可以找到这样一段:

    if (self.secondViewAttribute.view) {
        MAS_VIEW *closestCommonSuperview = [self.firstViewAttribute.view mas_closestCommonSuperview:self.secondViewAttribute.view];
        NSAssert(closestCommonSuperview,
                 @"couldn't find a common superview for %@ and %@",
                 self.firstViewAttribute.view, self.secondViewAttribute.view);
        self.installedView = closestCommonSuperview;
    } else if (self.firstViewAttribute.isSizeAttribute) {
        self.installedView = self.firstViewAttribute.view;
    } else {
        self.installedView = self.firstViewAttribute.view.superview;
    }

注意到,closetCommonSuperview就是Masonry为我们找到的最近公共父控件。

- (instancetype)mas_closestCommonSuperview:(MAS_VIEW *)view {
    MAS_VIEW *closestCommonSuperview = nil;

    MAS_VIEW *secondViewSuperview = view;
    while (!closestCommonSuperview && secondViewSuperview) {
        MAS_VIEW *firstViewSuperview = self;
        while (!closestCommonSuperview && firstViewSuperview) {
            if (secondViewSuperview == firstViewSuperview) {
                closestCommonSuperview = secondViewSuperview;
            }
            firstViewSuperview = firstViewSuperview.superview;
        }
        secondViewSuperview = secondViewSuperview.superview;
    }
    return closestCommonSuperview;
}

实现也比较简单。

至此,我们完成了所有准备,就可以开始愉快的自动布局啦。

以上就是Masonry对iOS自动布局封装的解读。

如有问题,欢迎指正。

问答
iOS:如何使用自动布局约束?
相关阅读
走进 Masonry
iOS自动布局框架之Masonry
iOS学习——布局利器Masonry框架源码深度剖析
【每日课程推荐】机器学习实战!快速入门在线广告业务及CTR相应知识

此文已由作者授权腾讯云+社区发布,更多原文请点击

搜索关注公众号「云加社区」,第一时间获取技术干货,关注后回复1024 送你一份技术课程大礼包!

海量技术实践经验,尽在云加社区

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

iOS自动布局——Masonry详解 的相关文章

  • SpringSecurity的使用和流程详解(二)

    文章目录 登录 准备工作 核心代码 校验 准备工作 核心代码 测试 退出登录 登录 准备工作 添加依赖
  • 线程池代码

    线程池 Global h pragma once const int DEFAULT POOL SIZE 10 const int STARTED 0 const int STOPPED 1 Mutex h pragma once incl

随机推荐

  • 鸡肋的RDP反制

    更新时间 2023年07月19日09 18 29 为什么叫鸡肋 鸡肋者 食之无肉 弃之有味 你说不能成吧 但是有成功案例 你说成了吧 要求太高 还要看运气的 一句话 对方需要开启磁盘共享 不开启 没办法反制 1 背景介绍 在很多攻防中 蓝队
  • 向量叉乘判断顺时针还是逆时针

    可以通过向量的叉乘判断一条线旋转的过程是顺时针还是逆时针的 有两个向量AB和AC 将两个向量进行叉乘 direct AB x AC 当direct gt 0时 为逆时针旋转 当direct lt 0时为顺时针旋转 当写类似于旋转按钮的控件的
  • 螺纹检测案例-螺距测量-大径小径检测-螺牙检测

    齿轮检测分析 Gear inspection and analysis 螺纹结构及检测要素 1 大径 宽径 2 小径 窄径 3 中经 4 螺距 间距 5 螺牙高度 深度 VisionBank 软件螺纹检测工具 02 01 螺纹检测分析流程
  • 高性能MySQL实战(二):索引

    我们在上篇 高性能MySQL实战 一 表结构 中已经建立好了表结构 这篇我们则是针对已有的表结构和搜索条件为表创建索引 1 根据搜索条件创建索引 我们还是先将表结构的初始化 SQL 拿过来 CREATE TABLE service log
  • 野火STM32F103教学视频完整目录(配合霸道-指南者开发板)

    野火STM32F103教学视频 P1 入门篇 2 如何安装KEIL5 flv 野火STM32F103教学视频 P2 入门篇 3 如何使用DAP仿真器下载程序 flv 野火STM32F103教学视频 P3 入门篇 4 串口ISP一键下载原理分
  • tmux使用方法

    tmux使用指南 比screen好用n倍 知乎 当你开启一个session的时候会默认开启一个window 这个截图就是一个window 而这个window可以拆成很多分subwindow 在这里就是 左上角Asubwindow 左下角Bs
  • 微信小程序优化多次跳转后卡顿问题

    一 微信小程序多次跳转会产生卡顿的原理 通过wx navigateTo 跳转 都会出现保留当前页面 打开新的页面机制 wx navigateTo不会将旧页面出栈 会将新页面入栈 栈内元素个数增加 栈内元素5个时 不能再跳转 手机性能好点 可
  • fastjson的JSONObject.toJSONString方法失效问题

    String toJSONString Object object 问题 该方法序列化的object对象 需要Object类中包含属性的get方法 如果没有get方法 则会序列化出一个空数组 解决 增加 Data注解 或 添加成员变量的ge
  • DOM编程2-重要案例!!!

    2 操作表格 2 1表格对象的属性和方法 2 2表格行对象的属性和方法 2 3表格单元格对象的属性和方法 3 案例演示 3 1表格案例 3 1 1修改标题样式 3 1 2在表格末尾追新增一行 3 1 3删除第二行 3 1 4复制最后一行 订
  • Docker命令指南

    文章目录 前言 一 基础命令 前言 记录一些基本的Docker命令 一 基础命令 docker version docker的详细信息 docker info docker的系统信息 docker help docker帮助手册 docke
  • CodeBlocks+wxWidgets

    之前也安装过CodeBlocks 只是当时没有安装wxWidgets 试着新建一个wxWidgets工程后没有看到界面设计的东东就放弃了 今天发现群里的南果梨也在用CodeBlocks 在他的帮助也终于成功的安装了wxWidgets 到ww
  • 008-黑盒测试和白盒测试的优缺点

    黑盒测试和白盒测试的优缺点 黑盒测试的优点有 比较简单 不需要了解程序内部的代码及实现 与软件的内部实现无关 从用户角度出发 能很容易的知道用户会用到哪些功能 会遇到哪些问题 基于软件开发文档 所以也能知道软件实现了文档中的哪些功能 在做软
  • js执行shell命令的几种方式(Node)

    在做一个客户端基建项目的时候 多处需要用到JS调取命令行执行shell脚本 这里对shell命令 JS执行shell命令做一个简单的介绍和总结 前言 一般在纯前端静态页面的项目中 是不需要JS调命令行操作的 通常在Node项目 或者在Ele
  • 超级详细的wordpress个人博客搭建教程(图文结合)!

    一 什么是云服务器 云服务器 ElasticComputeService ECS 是一种简单高效 安全可靠 处理能力可弹性伸缩的计算服务 其管理方式比物理服务器更简单高效 用户无需提前购买硬件 即可迅速创建或释放任意多台云服务器 云服务器帮
  • css里面的after_css after是什么?

    after是CSS的一种伪元素选择器 用于在被选元素的内容后面插入内容 通常会配合使用content属性来指定要插入的内容 after选择器的作用就是在指定的元素内容 而不是元素本身 之后插入一个包含content属性指定内容的行内元素 A
  • QT中的tcp一个服务器处理多个客户端

    客户端 我们先在头文件中 在头文件中添加 include
  • 提高C++性能的编程技术笔记:单线程内存池+测试代码

    频繁地分配和回收内存会严重地降低程序的性能 性能降低的原因在于默认的内存管理是通用的 应用程序可能会以某种特定的方式使用内存 并且为不需要的功能付出性能上的代价 通过开发专用的内存管理器可以解决这个问题 对专用内存管理器的设计可以从多个角度
  • 开源框架若依实战Demo学习笔记

    若依是个不错的框架 下面是我用他写的一个简单demo 以后会在此基础上进行进一步的开发 下面是整合的过程 我在整合的过程中可谓是一波三折 大家如果不想走弯路 那么这篇文章将对你很有帮助 首先 项目代码地址先列出来 方便大家下载使用 这是整理
  • GD32F103基础教程—硬件介绍(四)

    一 教程简介 本章主要是对MINI GD32F103C8T6 凌智基础开发 板的硬件进行介绍 以及一些对应的电路原理进行说明 对应的原理图以及生成的库文件都在资料文件里面有提供 可以直接使用 图1 设计原理图 二 相关事项 1 核心处理器
  • iOS自动布局——Masonry详解

    欢迎大家前往腾讯云 社区 获取更多腾讯海量技术实践干货哦 本文由鹅厂新鲜事儿发表于云 社区专栏 作者 oceanlong 腾讯 移动客户端开发工程师 前言 UI布局是整个前端体系里不可或缺的一环 代码的布局是设计语言与用户视觉感受沟通的桥梁