2016-02-12 77 views
1

我使用CosmicMind的快速材質庫。我試圖讓Menu示例與編程式自動佈局一起工作 - 我可以在哪裏找到如何使其工作的示例?CosmicMind Swift材質庫帶有堆疊菜單的編程式自動佈局

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子類,所以我不確定如何在自動佈局中使用它。

public class Menu { 
... 
} 

回答

1

你有什麼看法的按鈕?嘗試使用與您的按鈕大小相同的尺寸將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) 
    } 
} 
+0

非常感謝@jlichti!你是一個拯救生命的人!這非常有效,並允許我使用程序化自動佈局將菜單定位到需要的位置。 – DiscDev

+1

嗨,這個問題啓發了版本1.32.1中的更新,現在有一個MenuView :)有一個示例項目顯示它是如何工作的。 – CosmicMind

+0

@CosmicMind你搖滾,感謝偉大的自由! – DiscDev