2017-09-25 34 views
-7

a screenshot of my viewcontrollerXcode Swift:如何使UIButton创建一个新的对象?

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

我很感激,如果你们可以帮忙,谢谢!

+0

把你的代码放在这里 – Govaadiyo

+0

什么是笔记本?这是观点吗? –

+0

选中此项:https://www.appcoda.com/learnswift/build-your-first-app.html –

回答

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) 
} 

这可以让你走,但决不是正确的方法。正确的做法是增加约束条件以使其具有适应性,但这对您是好运!

相关问题