Giter Site home page Giter Site logo

Comments (9)

roberthein avatar roberthein commented on May 9, 2024

Hi @Andriyas123!

This is the problem: cancelButton.leading(to: saveButton)
The problem here is that you did add cancelButton as a subview of view but saveButton is not yet added to a superview at this point.

Let me know if this fixes your problem :)

from tinyconstraints.

roberthein avatar roberthein commented on May 9, 2024

@Andriyas123 There are some other issues too:

  • For every view you want to use with Auto Layout you have to set translatesAutoresizingMaskIntoConstraints to false.
  • For every view you want to use with Auto Layout you do not initialise the view with a frame, you can use UIView() or UIView(frame: .zero) instead.
  • There is also a redundancy: for cancelButton you set the leading edge to saveButton and for saveButton you set all edges including leading to cancelButton.

from tinyconstraints.

roberthein avatar roberthein commented on May 9, 2024

@Andriyas123 What is the layout you want to achieve here? Maybe I can help you with the setup.

from tinyconstraints.

Andriyas123 avatar Andriyas123 commented on May 9, 2024

@roberthein First of all, thank you for your explanation. It now makes more sense to me.

Secondly, I attach a screenshot of the first VC where constraints to all buttons have been applied in IB. (It is a Camera Preview VC, thus when user takes a photo, he is redirected to another VC that I set up programatically. It is represented by the second screenshot.)

For example, I need Draw Button to be in top right corner all of the time, while Save Button in lower left. When I run it on my iPhone 5, it is fine, but of course, when I attach iPhone 6 Plus, for instance, buttons in my second VC are completely messed up for understandable reasons.

That's what I want to achieve, basically. I hope I explained it well... Once again, @roberthein, thank you for your help!

1 vc all is fine
2 vc problems here

Code for buttons is below:

    func addButtons() {

    cancelButton = UIButton(frame: CGRect(x: 10.0, y: 10.0, width: 50.0, height: 50.0))
    cancelButton.layer.shadowOpacity = 0.7
    cancelButton.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
    cancelButton.layer.shadowRadius = 5.0
    cancelButton.showsTouchWhenHighlighted = true
    cancelButton.setImage(#imageLiteral(resourceName: "Close"), for: UIControlState())
    cancelButton.addTarget(self, action: #selector(cancel), for: .touchUpInside)
    view.addSubview(cancelButton)
    
    saveButton = UIButton (frame: CGRect(x: 10.0, y: 506.0, width: 50, height: 50))
    saveButton.layer.shadowOpacity = 0.7
    saveButton.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
    saveButton.layer.shadowRadius = 5.0
    saveButton.showsTouchWhenHighlighted = true
    saveButton.setImage(#imageLiteral(resourceName: "Save"), for: UIControlState())
    saveButton.addTarget(self, action: #selector(save), for: .touchUpInside)
    view.addSubview(saveButton)
    
    shareButton = UIButton (frame: CGRect(x: 240.0, y: 484.0, width: 60, height: 60))
    shareButton.layer.shadowOpacity = 0.7
    shareButton.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
    shareButton.layer.shadowRadius = 5.0
    shareButton.showsTouchWhenHighlighted = true
    shareButton.setImage(#imageLiteral(resourceName: "Share"), for: UIControlState())
    shareButton.addTarget(self, action: #selector(share), for: .touchUpInside)
    view.addSubview(shareButton)
    
    textButton = UIButton (frame: CGRect(x: 210.0, y: 10.0, width: 50, height: 50))
    textButton.layer.shadowOpacity = 0.7
    textButton.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
    textButton.layer.shadowRadius = 5.0
    textButton.showsTouchWhenHighlighted = true
    textButton.setImage(#imageLiteral(resourceName: "Text"), for: UIControlState())
    textButton.addTarget(self, action: #selector(handleTap), for: .touchUpInside)
    view.addSubview(textButton)
    
    drawButton = UIButton (frame: CGRect(x: 260.0, y: 10.0, width: 50, height: 50))
    drawButton.layer.shadowOpacity = 0.7
    drawButton.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
    drawButton.layer.shadowRadius = 5.0
    drawButton.showsTouchWhenHighlighted = true
    drawButton.setImage(#imageLiteral(resourceName: "Draw"), for: UIControlState())
    drawButton.addTarget(self, action: #selector(enableDraw), for: .touchUpInside)
    view.addSubview(drawButton)

}

from tinyconstraints.

roberthein avatar roberthein commented on May 9, 2024

First make sure you are using TinyConstraints 2.0
Initialise every button with UIButton(), no frames.
For every button add button.translatesAutoresizingMaskIntoConstraints = false

Then, after you created and added all the buttons, write this:

        let margin: CGFloat = 10
        let buttonSize = CGSize(width: 50, height: 50)
        let largeButtonSize = CGSize(width: 60, height: 60)
        
        cancelButton.size(buttonSize)
        cancelButton.top(to: view, offset: margin)
        cancelButton.left(to: view, offset: margin)
        
        saveButton.size(buttonSize)
        saveButton.bottom(to: view, offset: -margin)
        saveButton.left(to: view, offset: margin)
        
        shareButton.size(largeButtonSize)
        shareButton.bottom(to: view, offset: -margin)
        shareButton.right(to: view, offset: -margin)
        
        drawButton.size(buttonSize)
        drawButton.top(to: view, offset: margin)
        drawButton.right(to: view, offset: -margin)
        
        textButton.size(buttonSize)
        textButton.top(to: view, offset: margin)
        textButton.rightToLeft(of: drawButton, offset: -margin)

I hope this will help you on your way, but don't hesitate if you have more questions. :)

from tinyconstraints.

Andriyas123 avatar Andriyas123 commented on May 9, 2024

@roberthein Thank you! I have added the code, it works smoothly but need to try it on a different device later.

I will answer with my progress and will let you close this 'issue' ASAP. Thank you!

from tinyconstraints.

roberthein avatar roberthein commented on May 9, 2024

🤘

from tinyconstraints.

Andriyas123 avatar Andriyas123 commented on May 9, 2024

@roberthein Constraints work! You are a genius, thank you! This 'issue' can be closed.

from tinyconstraints.

roberthein avatar roberthein commented on May 9, 2024

No problem! Success!

from tinyconstraints.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.