Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

使用Swift自定义MMPopupView #51

Open
Lyansun opened this issue Oct 21, 2016 · 1 comment
Open

使用Swift自定义MMPopupView #51

Lyansun opened this issue Oct 21, 2016 · 1 comment

Comments

@Lyansun
Copy link

Lyansun commented Oct 21, 2016

你好,我在使用您的MMPopupView时遇到这个情况,自定义的View无法显示,一直找不到原因,以下是我的代码 能否麻烦您帮我看一下是不是哪里错了呢,感谢
`import UIKit
import ChameleonFramework
enum ShareType {
case TOPIC
case STATION
}
class ShareView: MMPopupView {
typealias cancelAc = ()
typealias confirmAc = () -> Void
internal var cancelBlock: cancelAc?
private var confirmBlock: confirmAc?
// 标题
private var titleLabel: UILabel = {
let label = UILabel()
label.textColor = FlatNavyBlue()
label.textAlignment = .Center
label.font = UIFont.systemFontOfSize(15)
label.layer.borderWidth = 0.3
label.layer.borderColor = FlatWhite().CGColor
label.backgroundColor = UIColor.vcMainColor()
label.layer.masksToBounds = true
return label
}()
// 确定 按钮
private var confirmBtn: UIButton = {
let confirmBtn = UIButton()
confirmBtn.titleLabel?.font = UIFont.systemFontOfSize(14)
confirmBtn.setTitleColor(FlatNavyBlueDark(), forState: .Normal)
confirmBtn.backgroundColor = UIColor.vcMainColor()
return confirmBtn
}()
// 图片
private var shareImageView: UIImageView = {
let imageView = UIImageView()
imageView.layer.cornerRadius = 5.0
imageView.layer.masksToBounds = true
return imageView
}()
// 正文标题
private var shareTitle: UILabel = {
let label = UILabel()
label.textColor = FlatBlack()
label.font = UIFont.systemFontOfSize(15)
return label
}()
// 正文
private var shareContentLabel: UILabel = {
let label = UILabel()
label.font = UIFont.systemFontOfSize(12)
label.textColor = FlatGray()
label.backgroundColor = UIColor.vcMainColor()
label.layer.cornerRadius = 1.0
label.layer.masksToBounds = true
return label
}()
// 标题 Title
internal var mainTitle: String? = "提示"{
didSet{
titleLabel.text = mainTitle
}
}
// ShareView背景色
internal var viewColor: UIColor? = UIColor.whiteColor(){
didSet{
self.backgroundColor = viewColor
}
}
// 确定按钮 Title
internal var confirmTitle: String? = "确定"{
didSet{
confirmBtn.setTitle(confirmTitle, forState: .Normal)
}
}
// 确定按钮 TitleColor
internal var confirmBtnTitleColor: UIColor? = FlatNavyBlueDark(){
didSet{
confirmBtn.setTitleColor(confirmBtnTitleColor, forState: .Normal)
}
}
override init(frame: CGRect) {
super.init(frame: frame)
}
// 取消按钮-事件
func cancel(){

}
// 确定按钮-事件
func confirm(){
    self.hide()
    confirmBlock!()
}
init(Type:ShareType,frame:CGRect,shareData:ShareData,confirmAction: (() -> Void)?) {
    super.init(frame: frame)
    print("MMPopup初始化\(frame)")
    self.type = MMPopupType.Custom
    self.frame = frame
    self.backgroundColor = viewColor
    self.layer.cornerRadius = 2.0
    self.layer.masksToBounds = true
    titleLabel.text = mainTitle

    confirmBlock = confirmAction
    confirmBtn.setTitle(confirmTitle, forState: .Normal)
    confirmBtn.addTarget(self, action: #selector(self.confirm), forControlEvents: .TouchUpInside)
    self.addSubview(titleLabel)
    //self.addSubview(cancelBtn)
    self.addSubview(confirmBtn)
    titleLabel.sd_layout()
        .topSpaceToView(self,0)
        .leftSpaceToView(self,0)
        .rightSpaceToView(self,0)
        .heightIs(30)
    confirmBtn.sd_layout()
        .leftSpaceToView(self,0)
        .rightSpaceToView(self,0)
        .bottomSpaceToView(self,0)
        .heightIs(35)
    switch Type {
    case .STATION:
        StationShareViewInitlizel(shareData)
        break
    case .TOPIC:
        TopicShareViewInitlizel(shareData)
        break
    }
}
required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
private func StationShareViewInitlizel(data:ShareData){
    shareImageView.sd_setImageWithURL(NSURL(string: data.shareImageUrlStr))
    self.addSubview(shareImageView)
    shareImageView.sd_layout()
        .topSpaceToView(titleLabel,5)
        .leftSpaceToView(self,5)
        .heightIs(self.frame.height - 35 - 30 - 15)
        .widthIs(self.frame.height - 35 - 30 - 15)
    shareTitle.text = data.shareTitle
    self.addSubview(shareTitle)
    shareTitle.sd_layout()
        .topEqualToView(shareImageView)
        .leftSpaceToView(shareImageView,5)
        .rightSpaceToView(self,5)
        .heightIs(16)
    shareContentLabel.text = data.shareContent
    self.addSubview(shareContentLabel)
    shareContentLabel.sd_layout()
        .topSpaceToView(shareTitle,10)
        .leftEqualToView(shareTitle)
        .rightEqualToView(shareTitle)
        .autoHeightRatio(0)
    shareContentLabel.setMaxNumberOfLinesToShow(3)
}
private func TopicShareViewInitlizel(data:ShareData){

}`
@Lyansun
Copy link
Author

Lyansun commented Oct 21, 2016

问题已经解决了,之所以不会显示出来是因为MMPopupView使用的是Masonry,需要mas_makeConstraints,而我使用的SDLayout没有这个功能,所以我使用SnapKit来设置snp_makeConstraints,就能正常显示了

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant