Comments (9)
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.
@Andriyas123 There are some other issues too:
- For every view you want to use with Auto Layout you have to set
translatesAutoresizingMaskIntoConstraints
tofalse
. - For every view you want to use with Auto Layout you do not initialise the view with a frame, you can use
UIView()
orUIView(frame: .zero)
instead. - There is also a redundancy: for
cancelButton
you set theleading
edge tosaveButton
and forsaveButton
you set all edges includingleading
tocancelButton
.
from tinyconstraints.
@Andriyas123 What is the layout you want to achieve here? Maybe I can help you with the setup.
from tinyconstraints.
@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!
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.
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.
@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.
🤘
from tinyconstraints.
@roberthein Constraints work! You are a genius, thank you! This 'issue' can be closed.
from tinyconstraints.
No problem! Success!
from tinyconstraints.
Related Issues (20)
- TinyConstraints cannot run in x86_64 simulator on Xcode 12/13 due to VALID_ARCHS built setting
- Fails to build for simulator running an Apple Silicon-based Mac HOT 2
- TinyConstraintsExample.app/Frameworks/SnapKit.framework/Info.plist: No such file or directory
- Center Multiplier HOT 1
- YouTube video tutorials HOT 1
- Documentation needs more examples. HOT 9
- TinyConstraints cannot run on simulator HOT 2
- edges(to:) works incorrectly HOT 1
- Video Documentation: New YouTube Playlist
- top()/bottom()/left()/right() lacks multiplier on offset amount HOT 1
- Naming suggestion HOT 1
- Distribution in stack HOT 2
- Nested Stack View HOT 1
- Allow setting `isActive` default value.
- View declaration conflicts with SwiftUI's View
- Please add support for margins HOT 1
- Aspect Ratio Constraint for UIImageView? HOT 1
- Stack function changes translatesAutoresizingMaskIntoConstraints of superview
- Implementations of `trailingToSuperview` and `leadingToSuperview` only work correct only for Left-to-Right languages / layout. HOT 2
- M1 MacBook Air - Not able to locate the module
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tinyconstraints.