if we're in the real pre-commit handler we can't actually add any new fences due to CA restriction - could this mean something here?
Don't worry about the fences warning message. It seems to be harmless and not caused by anything you're doing.
There are several problems with the code you posted:
You create
myView
and constrain its center, but you don't give it any size constraints. Furthermore,myView
is a local variable and you don't add any subviews tomyView
. SomyView
is an invisible, sizeless view with no contents. Why are you creating it at all?You're drawing your “uberCircle” using a bare shape layer. By “bare”, I mean there's no view whose
layer
property is that layer. Bare layers don't participate in autolayout.You compute the position of each button based on the center of the top-level view's bounds. You're doing this during
viewDidLoad
, butviewDidLoad
is called before the top-level view has been resized to fit the current device. So your wheel won't even be centered at launch on some devices.You don't set any constraints on the buttons, or set their autoresizing masks. The result is that, when the device rotates, the top-level view resizes but each button's position (relative to the top-left corner of the top-level view) stays the same.
Turning on the “Upside Down” checkbox is not sufficient to allow upside-down orientation on iPhones, only on iPads.
Here are the changes you need to make:
Use a view to draw the “uberCircle”. If you want to use a shape layer, make a subclass of
UIView
that uses aCAShapeLayer
for its layer. You can copy theShapeView
class from this answer.Set constraints from the center of the uberCircle to the center of the top-level view, to keep the uberCircle centered when the top-level view changes size.
For each button, set constraints from the center of the button to the center of the top-level view, to keep the button positioned properly when the top-level view changes size. These constraints need non-zero constants to offset the buttons from the center.
Override
supportedInterfaceOrientations
to enable upside-down orientation (in addition to checking the “Upside Down” checkbox).Get rid of
myView
inviewDidLoad
. You don't need it.Get rid of the
centre
property. You don't need it.
Thus:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
createUberCircle()
createButtons()
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask { return .all }
private let radius: CGFloat = 96
private let buttonCount = 10
private let buttonSideLength: CGFloat = 60
private func createUberCircle() {
let circle = ShapeView()
circle.translatesAutoresizingMaskIntoConstraints = false
circle.shapeLayer.path = UIBezierPath(ovalIn: CGRect(x: -radius, y: -radius, width: 2*radius, height: 2*radius)).cgPath
circle.shapeLayer.fillColor = UIColor.cyan.cgColor
view.addSubview(circle)
circle.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
circle.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
}
private func createButtons() {
for i in 1 ... buttonCount {
createButton(number: i)
}
}
private func createButton(number: Int) {
let button = UIButton(type: .custom)
button.translatesAutoresizingMaskIntoConstraints = false
button.backgroundColor = .white
button.layer.cornerRadius = buttonSideLength / 2
button.layer.borderWidth = 1
button.layer.borderColor = UIColor.black.cgColor
button.clipsToBounds = true
button.titleLabel!.font = UIFont.systemFont(ofSize: buttonSideLength / 2)
button.setTitleColor(.red, for: .normal)
button.setTitle(String(number), for: .normal)
view.addSubview(button)
let radians = 2 * CGFloat.pi * CGFloat(number) / CGFloat(buttonCount) - CGFloat.pi / 2
let xOffset = radius * cos(radians)
let yOffset = radius * sin(radians)
NSLayoutConstraint.activate([
button.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: xOffset),
button.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: yOffset),
button.widthAnchor.constraint(equalToConstant: buttonSideLength),
button.heightAnchor.constraint(equalToConstant: buttonSideLength)
])
}
}
class ShapeView: UIView {
override class var layerClass: Swift.AnyClass { return CAShapeLayer.self }
lazy var shapeLayer: CAShapeLayer = { self.layer as! CAShapeLayer }()
}
I want to add into a string that is surrounded by
Try this:
detailsLabel.text = " \"Hi\" "
Can't access a property in a Struct
The setup of your types is perfectly fine. However, when we look at the loop you are using:
func setUpMenuSubsection() {
for subsection in restaurantMenuInfo.menuInfo.subsections.subsectionName { // <-- this line
if let subsectionView = Bundle.main.loadNibNamed("SubsectionView", owner: nil, options: nil)?.first as? SubsectionView {
subsectionView.setUpSubsection(subsection: subsection)
stackSubsections.addArrangedSubview(subsectionView)
}
}
}
The error is on the marked line. That is because subsections
, which is of type [Subsection]
, does not have a property subsectionName
. If you just want to loop over the subsections (which is probably what you want), you can simply remove .subsectionName
, and then to access the name of each subsection, inside the loop you would say subsection.subsectionName
like so:
func setUpMenuSubsection() {
for subsection in restaurantMenuInfo.menuInfo.subsections {
print(subsection.subsectionName)
}
}
If, however, you want to loop only over the names, and do not care about the subsections themselves, you can do this:
func setUpMenuSubsection() {
for subsectionName in restaurantMenuInfo.menuInfo.subsections.map(\.subsectionName) {
print(subsectionName)
}
}
Related Topics
How to Change Wkwebview or Uiwebview Default Font
How to Load an Uiimage into a Swiftui Image Asynchronously
How to Display Activity Indicator in Center of Uialertcontroller
Using Nsuserdefaults with Xcode 8 and iOS 10
How to Make a Button Continually Call a Function When Held Down (Spritekit)
Lazy Loading Data in iOS Carplay
How to Add Images for Different Screen Size from Assets.Xcassets in Xcode 8
Optional Binding with Try? and As? Still Produces an Optional Type
Uiimagejpegrepresentation Returns Nil
How to Rotate Orientation Programmatically in Swift
Open Phone Settings Programmatically in iOS9
Swift - Clipping Image with Uibezierpath
Trying to Set Only Time in Uidatepicker in Swift 2.0
Performance Testing in Swift Using Tdd
Sprite Moves Two Places After Being Paused and Then Unpaused
Uibutton Image for Normal State in Collectionview Cell Repeats Itself Every Four Cells