Xcode Swift:如何让UIButton创建一个新对象?

时间:2017-09-25 11:44:57

标签: ios swift xcode uibutton storyboard

a screenshot of my viewcontroller

我想要我的"添加新笔记本"每当我点击它时,右上角的按钮就会创建一个新的笔记本。我还希望将新笔记本放在正确的位置。我还没有编写任何代码,因为我不知道如何开始编码。我知道我必须为按钮创建一个IBAction,但我不知道下一步是什么。

如果你们能提供帮助,我感激不尽,谢谢!

1 个答案:

答案 0 :(得分:0)

@IBOutlet weak var firstNotebook: UIView! // Make a ref to the first notebook

@IBAction func addNotebook(_ sender: UIButton) { 
    let newNotebook = UIView(frame: firstNotebook.frame)  // This is making the new notebook using the first ones frame only thing is you would be putting it right over the first one so you need to make a new X position 
    let newX = firstNotebook.frame.origin.x + 40.0 // I have no idea of how far you want it 
    newNotebook.frame.origin.x = newX
    // So you have a new UIView now, you still need to put it in the superView, the superView is already named view for you

    view.addSubview(newNotebook)
}

这可以让你前进,但绝不是正确的方法。正确的方法是添加约束来实现这种适应性,但这对你来说是一个好运!