Search code examples
swiftmasonry-ios-osx

use masonry in swift and objectiveC mix project


I have a objective-C and Swift mix project. I m using Masonry library for autolayout. I m able to use masonry in Objective C code but not in swift. How to do that ?


Solution

  • Create bridging header file if you haven't yet, and paste following line

    #import "Masonry.h"
    

    If you are using CocoaPods to add Masonry to your project, you might see error Masonry.h file not found, this is because you need to set User Header Search Paths, to do so goto TARGETS > Build Settings and paste following

    //:configuration = Debug
    USER_HEADER_SEARCH_PATHS = pods/**
    
    //:configuration = Release
    USER_HEADER_SEARCH_PATHS = pods/**
    
    //:completeSettings = some
    USER_HEADER_SEARCH_PATHS
    

    Screenshot to make sure User Header Search Paths is properly set

    enter image description here

    Note After setting above header, you do not need import Masonry line in your swift file

    Usage

    UIView.mas_makeConstraints { (make:MASConstraintMaker!) in
        make.centerY.mas_equalTo()(anotherView)
        make.left.mas_equalTo()(15)
        make.height.mas_equalTo()(30)
        make.width.mas_equalTo()(30)
    }