CosmicMind Swift材质库程序化自动布局,带有堆叠菜单

时间:2016-02-12 22:33:22

标签: ios swift autolayout cosmicmind material-swift

我正在使用CosmicMind的材质库来实现swift。我正在尝试使用Menu示例来使用程序化自动布局 - 我在哪里可以找到如何使其工作的示例?

Github上的例子中我没有找到使用自动布局的方法。

/// Prepares the FlatMenu example.
private func prepareFlatbMenuExample() {
    let btn1: FlatButton = FlatButton()
    btn1.addTarget(self, action: "handleFlatMenu", forControlEvents: .TouchUpInside)
    btn1.setTitleColor(MaterialColor.white, forState: .Normal)
    btn1.backgroundColor = MaterialColor.blue.accent3
    btn1.pulseColor = MaterialColor.white
    btn1.setTitle("Sweet", forState: .Normal)
    view.addSubview(btn1)

    let btn2: FlatButton = FlatButton()
    btn2.setTitleColor(MaterialColor.blue.accent3, forState: .Normal)
    btn2.borderColor = MaterialColor.blue.accent3
    btn2.pulseColor = MaterialColor.blue.accent3
    btn2.borderWidth = .Border1
    btn2.setTitle("Good", forState: .Normal)
    view.addSubview(btn2)

    let btn3: FlatButton = FlatButton()
    btn3.setTitleColor(MaterialColor.blue.accent3, forState: .Normal)
    btn3.borderColor = MaterialColor.blue.accent3
    btn3.pulseColor = MaterialColor.blue.accent3
    btn3.borderWidth = .Border1
    btn3.setTitle("Nice", forState: .Normal)
    view.addSubview(btn3)

    // Initialize the menu and setup the configuration options.
    flatMenu = Menu(origin: CGPointMake(spacing, view.bounds.height - height - spacing))
    flatMenu.direction = .Down
    flatMenu.spacing = 8
    flatMenu.buttonSize = CGSizeMake(120, height)
    flatMenu.buttons = [btn1, btn2, btn3]
}

flatMenu属于材质库中的Menu类型,是一个包含菜单中每个按钮的类。看来“起源”是控制页面上定位的因素,但由于Menu不是UIView的子类,我不知道如何在autolayout中使用它。

public class Menu {
...
}

1 个答案:

答案 0 :(得分:1)

你有什么视图按钮?尝试使用与按钮大小相同的尺寸将Superview定位到AutoLayout,然后将菜单的原点设置为CGRectZero。

还要确保将superview的clipsToBounds设置为false(默认值)。并且因为按钮超出了自动布局放置视图的范围,所以您需要使用下面的自定义UIView子类来处理按钮上的操作。

class MenuParentView: UIView {

  override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {

    //because the subviews will be outside the bounds of this view
    //we need to look at the subviews to see if we have a hit
    for subview in self.subviews {
        let pointForTargetView = subview.convertPoint(point, fromView: self)
        if CGRectContainsPoint(subview.bounds, pointForTargetView) {
            return subview.hitTest(pointForTargetView, withEvent: event)
        }
    }

    return super.hitTest(point, withEvent: event)
  }
}