Storyboard 引用 Xib

最近打算使用 Storyboard 開(kāi)發(fā)項(xiàng)目,但某些場(chǎng)景會(huì)用到 Storyboard 中引用 Xib,根據(jù)網(wǎng)上教程搞了一翻,但由于環(huán)境各有差異,按網(wǎng)上教程沒(méi)弄出來(lái),后來(lái)發(fā)現(xiàn)可能是 IB 更新的原因,新版 IB 中沒(méi)有 widthable 和 heightable 屬性,所以導(dǎo)致要復(fù)用 Xib 時(shí),AutoLayout 無(wú)效。
自己寫(xiě)了個(gè)基類(lèi)來(lái)解決這個(gè)問(wèn)題, OC 版的項(xiàng)目在 Github 上,點(diǎn)擊前往
pod 'YYNib'
Swift 版本 點(diǎn)擊前往
pod 'YYNib-swift'
兩個(gè)工程中可以使用Pod引入,注意: 如果需要支持 iOS7 的話,swift 版本不能使用 Pod 引入。
OC主要代碼如下

#import "YYNibView.h"

@implementation YYNibView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initSubviews];
        
        if (CGRectIsEmpty(frame)) {
            self.frame = _contentView.bounds;
        } else {
            _contentView.frame = self.bounds;
        }
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self initSubviews];
        _contentView.frame = self.bounds;
    }
    return self;
}

- (void)setFrame:(CGRect)frame {
    [super setFrame:frame];
    _contentView.frame = self.bounds;
}

- (void)setBackgroundColor:(UIColor *)backgroundColor {
    [super setBackgroundColor:backgroundColor];
    _contentView.backgroundColor = backgroundColor;
}

- (void)initSubviews {
    NSString *className = NSStringFromClass([self class]);
    _contentView = [[NSBundle mainBundle] loadNibNamed:className owner:self options:nil].firstObject;
    _contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self addSubview:_contentView];
    // Fix backgroundColor
    self.backgroundColor = _contentView.backgroundColor;
}

@end

Swift 主要代碼如下

import UIKit

class YYNibView: UIView {
    
    var contentView:UIView!;
    
    override init(frame: CGRect) {
        super.init(frame: frame);
        
        self.initWithSubviews();
        if (CGRectIsEmpty(frame)) {
            self.frame = (contentView?.bounds)!;
        } else {
            contentView?.frame = self.bounds;
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder);
        self.initWithSubviews();
        self.contentView?.frame = self.bounds;
    }
    
    override var frame: CGRect {
        didSet {
            self.contentView?.frame = self.bounds;
        }
    }
    
    override var backgroundColor: UIColor? {
        didSet {
            self.contentView?.backgroundColor = self.backgroundColor;
        }
    }
    
    func initWithSubviews() {
        let className = "\(self.classForCoder)"
        self.contentView = NSBundle.mainBundle().loadNibNamed(className, owner: self, options: nil).first as? UIView;
        self.contentView?.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight];
        self.addSubview(self.contentView!);
        // Fix backgroundColor
        self.backgroundColor = self.contentView?.backgroundColor;
    }
    
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 得知有《降臨》這部電影上映是在OMT冥想導(dǎo)師群里有位同修提到為了看懂這部電影專門(mén)掛在409層阿卡西幸虧學(xué)習(xí)了冥想課...
    承謙閱讀 382評(píng)論 0 0
  • 我曾多次說(shuō)過(guò),教師的最大師德是無(wú)私和按教育規(guī)律辦事,即“愛(ài)與會(huì)愛(ài)?!比绻靡粋€(gè)詞來(lái)概括,那就是良心。 ...
    經(jīng)史合參閱讀 601評(píng)論 0 2
  • 好失落,為什么總是找那個(gè)萎了的家伙聊天呢,從這星期開(kāi)始我再也不和他聊天了
    如一不如一閱讀 145評(píng)論 0 0
  • 呆在家里真的很無(wú)聊,下午抽空畫(huà)著貓。雖然畫(huà)了很久,但是一直沒(méi)能靜下心,所以細(xì)節(jié)處還是比較粗糙。繼續(xù)努力加油
    半斤v5閱讀 224評(píng)論 0 0
  • 我們是同學(xué),但是經(jīng)過(guò)這樣的光陰以后,我不知道我們畢業(yè)以后會(huì)不會(huì)互刪退圈。 1.人都是有自尊底線的 她一進(jìn)寢室給我的...
    愛(ài)金桔的魚(yú)閱讀 1,781評(píng)論 0 0

友情鏈接更多精彩內(nèi)容