I have a UIButton
inside a UIView
, my UIButton
has constraints which I set in storyboard. Now, I want to set the center of the UIButton
at the center of the UIView
. How will I do that programmatically?
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- SwiftUI: UIImage (QRCode) does not load after call
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- Using if let syntax in switch statement
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Enum with associated value conforming to CaseItera
- Swift - hide pickerView after value selected
- Is there a Github markdown language identifier for
Try
.center
property likeYou can also specify
x
andy
If you need.This approach is using Using NSLayoutConstraint where self.cenBut is the IBoutlet for your button.
In viewDidLoad()
since you are using constraints, you won't be able to set the center so you need to use constraints to set the center as well:
I'm assuming that you know what "programmatically" means with autolayout constraints. If you are truly using autolayout then you must have set the properties of your button to setTranslatesAutoresizingMaskIntoConstraints to false.
I should be
But keep in mind that if you programmatically set the frame of a view, your layout constraints might not work as intended.