使用标签和按钮创建自定义popuser(swift / ios)

时间:2016-02-25 19:11:10

标签: ios swift

我尝试使用标签和按钮创建自定义popopview,如下所示;

custom popop

首先,创建一个带有opac背景的标签;

    let opacLabel = UILabel(frame:CGRectMake(0,0,self.screenWidth,self.screenHeight))
    opacLabel.backgroundColor = UIColor.lightGrayColor().colorWithAlphaComponent(0.9)
    view.addSubview(opacLabel)

然后是弹出视图的图层;

    let categoryMenuLabel = UILabel(frame: CGRectMake(10,40,100,300)) 
    categoryMenuLabel.backgroundColor = UIColor.whiteColor()
    categoryMenuLabel.clipsToBounds = true
    categoryMenuLabel.layer.cornerRadius = 7

然后按一下按钮

    let saveButton = UIButton(frame: CGRectMake(spaceX,400,150,40))
    saveButton.backgroundColor = UIColor.lightGrayColor()
    saveButton.titleLabel!.font = UIFont(name: "Helvetia", size: 25)
    saveButton.setTitle("Save", forState: UIControlState.Normal)
    saveButton.addTarget(self, action: "categorySaveButtonPressed:", forControlEvents: UIControlEvents.TouchUpInside)
    categoryMenuLabel.addSubview(saveButton)

最后将categoryMenuLabel添加到主视图并更改了顺序;

    self.view.addSubview(categoryMenuLabel)
    self.view.bringSubviewToFront(opacLabel)
    self.view.bringSubviewToFront(categoryMenuLabel)

使用这种配置,当我尝试按下按钮而不是按钮时,它按下标签下的表格单元格。 我也无法在标签之外禁用触摸操作。

1 个答案:

答案 0 :(得分:1)

只需在标签上启用用户互动即可。它默认是关闭的,这也阻止了它的孩子接收任何用户交互:

categoryMenuLabel.userInteractionEnabled = true