Swift animating view height - swift

I am trying to chain 2 animations. But can not get the second animation (change of height) working.
1st try:
self.customView.frame = CGRect(x: 20, y: 0, width: width, height: 76)
UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 1, options: .curveEaseInOut, animations: {
self.customView.frame = CGRect(x: 20, y: 20, width: 280, height: 76)
}, completion: { (finished: Bool) in
UIView.animate(withDuration: 0.3) {
self.customView.frame = CGRect(x: 20, y: 20, width: 280, height: 136)
}
})
2nd one:
self.customView.frame = CGRect(x: 20, y: 0, width: width, height: 76)
UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 1, options: .curveEaseInOut, animations: {
self.customView.frame = CGRect(x: 20, y: 20, width: 280, height: 76)
}, completion: { (finished: Bool) in
UIView.animate(withDuration: 0.3) {
var heightConstraint = self.customView.heightAnchor.constraint(equalToConstant: 136)
heightConstraint.isActive = true
self.view.layoutIfNeeded()
}
})

Related

Circle stuck at top left hand corner of screen swiftUI

I'm trying to draw a circle in SwiftUI at a constantly changing variable poseEstimator.bodyparts[.leftshoulder]!.location of type CGPoint on my View, and the end goal is that when the variable changes, I want the circle on the screen to move to the new coordinates stored in the variable.
I read up online on how to draw a circle in swiftUI and found out that you need to use the Circle() class to draw a circle, so that's what I did, but for some reason, my circle is always stuck at top left hand corner of the screen(0.0, 0.0), and even when I change the .position property of the circle to CGPoint(0.5,0.5), the circle is still stuck at the exact same location as before.
This is my swiftUI view struct:
import SwiftUI
struct StickFigureView: View { //declare the stickfigure for pose esimation as a swift view
#ObservedObject var poseEstimator: PoseEstimator
var size: CGSize
var body: some View {
if poseEstimator.bodyParts.isEmpty == false { //add in the sticks only if there are VNPoints stored in "bodyparts"
ZStack {
// Right leg
if poseEstimator.bodyParts[.rightAnkle]!.location != CGPoint(x: 0.0, y: 1.0) && poseEstimator.bodyParts[.rightKnee]!.location != CGPoint(x: 0.0, y: 1.0){
Stick(points: [poseEstimator.bodyParts[.rightAnkle]!.location,
poseEstimator.bodyParts[.rightKnee]!.location], size: size)
.stroke(lineWidth: 5.0)
.fill(Color.blue)
}
Circle()
.fill(Color.blue)
.frame(width: 10, height: 10)
.position(CGPoint(x: 0.5, y: 0.5)) // <- this is the circle which I have problems with
if poseEstimator.bodyParts[.rightKnee]!.location != CGPoint(x: 0.0, y: 1.0) && poseEstimator.bodyParts[.rightHip]!.location != CGPoint(x: 0.0, y: 1.0) && poseEstimator.bodyParts[.root]!.location != CGPoint(x: 0.0, y: 1.0) {
Stick(points: [poseEstimator.bodyParts[.rightKnee]!.location,
poseEstimator.bodyParts[.rightHip]!.location,
poseEstimator.bodyParts[.root]!.location], size: size)
.stroke(lineWidth: 5.0)
.fill(Color.blue)
}
// Left leg
if poseEstimator.bodyParts[.leftAnkle]!.location != CGPoint(x: 0.0, y: 1.0) && poseEstimator.bodyParts[.leftKnee]!.location != CGPoint(x: 0.0, y: 1.0){
Stick(points: [poseEstimator.bodyParts[.leftAnkle]!.location,
poseEstimator.bodyParts[.leftKnee]!.location], size: size)
.stroke(lineWidth: 5.0)
.fill(Color.blue)
}
if poseEstimator.bodyParts[.leftKnee]!.location != CGPoint(x: 0.0, y: 1.0) && poseEstimator.bodyParts[.leftHip]!.location != CGPoint(x: 0.0, y: 1.0) && poseEstimator.bodyParts[.root]!.location != CGPoint(x: 0.0, y: 1.0){
Stick(points: [poseEstimator.bodyParts[.leftKnee]!.location,
poseEstimator.bodyParts[.leftHip]!.location,
poseEstimator.bodyParts[.root]!.location], size: size)
.stroke(lineWidth: 5.0)
.fill(Color.blue)
}
// Right arm
if poseEstimator.bodyParts[.rightWrist]!.location != CGPoint(x: 0.0, y: 1.0) && poseEstimator.bodyParts[.rightElbow]!.location != CGPoint(x: 0.0, y: 1.0){
Stick(points: [poseEstimator.bodyParts[.rightWrist]!.location,
poseEstimator.bodyParts[.rightElbow]!.location], size: size)
.stroke(lineWidth: 5.0)
.fill(Color.blue)
}
if poseEstimator.bodyParts[.rightWrist]!.location != CGPoint(x: 0.0, y: 1.0) && poseEstimator.bodyParts[.rightElbow]!.location != CGPoint(x: 0.0, y: 1.0) && poseEstimator.bodyParts[.neck]!.location != CGPoint(x: 0.0, y: 1.0){
Stick(points: [poseEstimator.bodyParts[.rightElbow]!.location,
poseEstimator.bodyParts[.rightShoulder]!.location,
poseEstimator.bodyParts[.neck]!.location], size: size)
.stroke(lineWidth: 5.0)
.fill(Color.blue)
}
// Left arm
if poseEstimator.bodyParts[.leftWrist]!.location != CGPoint(x: 0.0, y: 1.0) && poseEstimator.bodyParts[.leftElbow]!.location != CGPoint(x: 0.0, y: 1.0){
Stick(points: [poseEstimator.bodyParts[.leftWrist]!.location,
poseEstimator.bodyParts[.leftElbow]!.location], size: size)
.stroke(lineWidth: 5.0)
.fill(Color.blue)
}
if poseEstimator.bodyParts[.leftElbow]!.location != CGPoint(x: 0.0, y: 1.0) && poseEstimator.bodyParts[.leftShoulder]!.location != CGPoint(x: 0.0, y: 1.0) && poseEstimator.bodyParts[.neck]!.location != CGPoint(x: 0.0, y: 1.0){
dot(Position: CGPoint(x: 0.5, y: 0.5), Color: Color.blue)
.position(CGPoint(x: 0.5, y: 0.5))
}
// Root to nose
if poseEstimator.bodyParts[.root]!.location != CGPoint(x: 0.0, y: 1.0) && poseEstimator.bodyParts[.neck]!.location != CGPoint(x: 0.0, y: 1.0){
Stick(points: [poseEstimator.bodyParts[.root]!.location,
poseEstimator.bodyParts[.neck]!.location], size: size)
.stroke(lineWidth: 5.0)
.fill(Color.blue)
}
}
}
}
}
Ok I've finally cracked the code. #jnpdx was right, the CGPoint that I set was too small to be even rendered in the first place. Upon further troubleshooting, I found out that the variable poseEstimator.bodyparts[.leftshoulder]!.location that I assigned to the position property of the circle was around 0.3, which was too small to be seen. I had to scale the variable using CGAffineTransform so that the CGpoint was relative to the width and height of the screen. This fixed the problem.
import SwiftUI
#available(iOS 15.0, *)
struct dotview: View { //declare the stickfigure for pose esimation as a swift view
#ObservedObject var poseEstimator: PoseEstimator
var size: CGSize
var body: some View {
if poseEstimator.bodyParts.isEmpty == false { //add in the sticks only if there are VNPoints stored in "bodyparts"
ZStack {
// Right leg
if poseEstimator.bodyParts[.rightAnkle]!.location != CGPoint(x: 0.0, y: 1.0) && poseEstimator.bodyParts[.rightKnee]!.location != CGPoint(x: 0.0, y: 1.0){
Circle()
.fill(poseEstimator.rightanklecolor)
.position(poseEstimator.bodyParts[.rightAnkle]!.location.applying(CGAffineTransform.identity.scaledBy(x: size.width, y: size.height)) .applying(CGAffineTransform(scaleX: -1, y: -1).translatedBy(x: -size.width, y: -size.height)))
.frame(width: 15, height: 15)
Circle()
.fill(poseEstimator.rightkneecolor)
.position(poseEstimator.bodyParts[.rightKnee]!.location.applying(CGAffineTransform.identity.scaledBy(x: size.width, y: size.height)) .applying(CGAffineTransform(scaleX: -1, y: -1).translatedBy(x: -size.width, y: -size.height)))
.frame(width: 15, height: 15)
}
if poseEstimator.bodyParts[.rightKnee]!.location != CGPoint(x: 0.0, y: 1.0) && poseEstimator.bodyParts[.rightHip]!.location != CGPoint(x: 0.0, y: 1.0) && poseEstimator.bodyParts[.root]!.location != CGPoint(x: 0.0, y: 1.0) {
Circle()
.fill(poseEstimator.rightkneecolor)
.position(poseEstimator.bodyParts[.rightKnee]!.location.applying(CGAffineTransform.identity.scaledBy(x: size.width, y: size.height)) .applying(CGAffineTransform(scaleX: -1, y: -1).translatedBy(x: -size.width, y: -size.height)))
.frame(width: 15, height: 15)
Circle()
.fill(poseEstimator.righthipcolor)
.position(poseEstimator.bodyParts[.rightHip]!.location.applying(CGAffineTransform.identity.scaledBy(x: size.width, y: size.height)) .applying(CGAffineTransform(scaleX: -1, y: -1).translatedBy(x: -size.width, y: -size.height)))
.frame(width: 15, height: 15)
}
// Left leg
if poseEstimator.bodyParts[.leftAnkle]!.location != CGPoint(x: 0.0, y: 1.0) && poseEstimator.bodyParts[.leftKnee]!.location != CGPoint(x: 0.0, y: 1.0){
Circle()
.fill(poseEstimator.leftanklecolor)
.position(poseEstimator.bodyParts[.leftAnkle]!.location.applying(CGAffineTransform.identity.scaledBy(x: size.width, y: size.height)) .applying(CGAffineTransform(scaleX: -1, y: -1).translatedBy(x: -size.width, y: -size.height)))
.frame(width: 15, height: 15)
Circle()
.fill(poseEstimator.leftkneecolor)
.position(poseEstimator.bodyParts[.leftKnee]!.location.applying(CGAffineTransform.identity.scaledBy(x: size.width, y: size.height)) .applying(CGAffineTransform(scaleX: -1, y: -1).translatedBy(x: -size.width, y: -size.height)))
.frame(width: 15, height: 15)
}
if poseEstimator.bodyParts[.leftKnee]!.location != CGPoint(x: 0.0, y: 1.0) && poseEstimator.bodyParts[.leftHip]!.location != CGPoint(x: 0.0, y: 1.0) && poseEstimator.bodyParts[.root]!.location != CGPoint(x: 0.0, y: 1.0){
Circle()
.fill(poseEstimator.leftkneecolor)
.position(poseEstimator.bodyParts[.leftKnee]!.location.applying(CGAffineTransform.identity.scaledBy(x: size.width, y: size.height)) .applying(CGAffineTransform(scaleX: -1, y: -1).translatedBy(x: -size.width, y: -size.height)))
.frame(width: 15, height: 15)
Circle()
.fill(poseEstimator.lefthipcolor)
.position(poseEstimator.bodyParts[.leftHip]!.location.applying(CGAffineTransform.identity.scaledBy(x: size.width, y: size.height)) .applying(CGAffineTransform(scaleX: -1, y: -1).translatedBy(x: -size.width, y: -size.height)))
.frame(width: 15, height: 15)
}
// Right arm
if poseEstimator.bodyParts[.rightWrist]!.location != CGPoint(x: 0.0, y: 1.0) && poseEstimator.bodyParts[.rightElbow]!.location != CGPoint(x: 0.0, y: 1.0){
Circle()
.fill(poseEstimator.rightwristcolor)
.position(poseEstimator.bodyParts[.rightWrist]!.location.applying(CGAffineTransform.identity.scaledBy(x: size.width, y: size.height)) .applying(CGAffineTransform(scaleX: -1, y: -1).translatedBy(x: -size.width, y: -size.height)))
.frame(width: 15, height: 15)
Circle()
.fill(poseEstimator.rightelbowcolor)
.position(poseEstimator.bodyParts[.rightElbow]!.location.applying(CGAffineTransform.identity.scaledBy(x: size.width, y: size.height)) .applying(CGAffineTransform(scaleX: -1, y: -1).translatedBy(x: -size.width, y: -size.height)))
.frame(width: 15, height: 15)
}
if poseEstimator.bodyParts[.rightElbow]!.location != CGPoint(x: 0.0, y: 1.0) && poseEstimator.bodyParts[.rightShoulder]!.location != CGPoint(x: 0.0, y: 1.0) && poseEstimator.bodyParts[.neck]!.location != CGPoint(x: 0.0, y: 1.0){
Circle()
.fill(poseEstimator.rightelbowcolor)
.position(poseEstimator.bodyParts[.rightElbow]!.location.applying(CGAffineTransform.identity.scaledBy(x: size.width, y: size.height)) .applying(CGAffineTransform(scaleX: -1, y: -1).translatedBy(x: -size.width, y: -size.height)))
.frame(width: 15, height: 15)
Circle()
.fill(poseEstimator.rightshouldercolor)
.position(poseEstimator.bodyParts[.rightShoulder]!.location.applying(CGAffineTransform.identity.scaledBy(x: size.width, y: size.height)) .applying(CGAffineTransform(scaleX: -1, y: -1).translatedBy(x: -size.width, y: -size.height)))
.frame(width: 15, height: 15)
}
// Left arm
if poseEstimator.bodyParts[.leftWrist]!.location != CGPoint(x: 0.0, y: 1.0) && poseEstimator.bodyParts[.leftElbow]!.location != CGPoint(x: 0.0, y: 1.0){
Circle()
.fill(poseEstimator.leftwristcolor)
.position(poseEstimator.bodyParts[.leftWrist]!.location.applying(CGAffineTransform.identity.scaledBy(x: size.width, y: size.height)) .applying(CGAffineTransform(scaleX: -1, y: -1).translatedBy(x: -size.width, y: -size.height)))
.frame(width: 15, height: 15)
Circle()
.fill(poseEstimator.leftelbowcolor)
.position(poseEstimator.bodyParts[.leftElbow]!.location.applying(CGAffineTransform.identity.scaledBy(x: size.width, y: size.height)) .applying(CGAffineTransform(scaleX: -1, y: -1).translatedBy(x: -size.width, y: -size.height)))
.frame(width: 15, height: 15)
}
if poseEstimator.bodyParts[.leftElbow]!.location != CGPoint(x: 0.0, y: 1.0) && poseEstimator.bodyParts[.leftShoulder]!.location != CGPoint(x: 0.0, y: 1.0) && poseEstimator.bodyParts[.neck]!.location != CGPoint(x: 0.0, y: 1.0){
Circle()
.fill(poseEstimator.leftelbowcolor)
.position(poseEstimator.bodyParts[.leftElbow]!.location.applying(CGAffineTransform.identity.scaledBy(x: size.width, y: size.height)) .applying(CGAffineTransform(scaleX: -1, y: -1).translatedBy(x: -size.width, y: -size.height)))
.frame(width: 15, height: 15)
Circle()
.fill(poseEstimator.leftshouldercolor)
.position(poseEstimator.bodyParts[.leftShoulder]!.location.applying(CGAffineTransform.identity.scaledBy(x: size.width, y: size.height)) .applying(CGAffineTransform(scaleX: -1, y: -1).translatedBy(x: -size.width, y: -size.height)))
.frame(width: 15, height: 15)
}
// Root to nose
if poseEstimator.bodyParts[.root]!.location != CGPoint(x: 0.0, y: 1.0) && poseEstimator.bodyParts[.neck]!.location != CGPoint(x: 0.0, y: 1.0){
Circle()
.fill(poseEstimator.rootcolor)
.position(poseEstimator.bodyParts[.root]!.location.applying(CGAffineTransform.identity.scaledBy(x: size.width, y: size.height)) .applying(CGAffineTransform(scaleX: -1, y: -1).translatedBy(x: -size.width, y: -size.height)))
.frame(width: 15, height: 15)
Circle()
.fill(poseEstimator.neckcolor)
.position(poseEstimator.bodyParts[.neck]!.location.applying(CGAffineTransform.identity.scaledBy(x: size.width, y: size.height)) .applying(CGAffineTransform(scaleX: -1, y: -1).translatedBy(x: -size.width, y: -size.height)))
.frame(width: 15, height: 15)
}
}
}
}
}

Optimal number of clusters - Error in FUNcluster(x, i, ...) : more cluster centers than distinct data points

I have these data and I need to find the optimal number of clusters of this table.
The values can be either 0, 0.5 or 1
library(NbClust)
library(factoextra)
library(pheatmap)
tab=structure(list(`57-B1` = c(1, 0.5, 0.5, 1, 1, 0.5), `57-B3` = c(0.5,
0.5, 0.5, 0, 0.5, 0.5), `57-C1` = c(1, 0.5, 0.5, 0.5, 1, 0.5),
`57-C5` = c(1, 0.5, 0.5, 1, 1, 1), `57-H2` = c(1, 0.5, 0.5,
0, 1, 1), `57-H4` = c(0.5, 0.5, 0.5, 0, 0.5, 0.5), `61-1-B1` = c(0.5,
0.5, 0.5, 0, 0.5, 0.5), `61-1-C2` = c(0.5, 0.5, 0.5, 0, 0.5,
0.5), `61-1-C5` = c(0.5, 0.5, 0.5, 0, 0.5, 0.5), `61-1-H1` = c(0.5,
0.5, 0, 0, 0.5, 0.5), `61-1-H3` = c(0.5, 0.5, 0.5, 0, 0.5,
0.5), `61-1-H5` = c(0.5, 0.5, 0, 0.5, 0.5, 0.5), `62-2_H2` = c(0.5,
0.5, 0.5, 0, 0.5, 0.5), `62_1_C2` = c(0.5, 0.5, 0, 0.5, 0.5,
0.5), `62_1_C5` = c(0.5, 0.5, 0.5, 0, 0.5, 0.5), `FL-39-C3` = c(0.5,
0.5, 0.5, 0, 0.5, 0.5), `FL-41-1-C3` = c(0.5, 0.5, 0.5, 0,
0.5, 0.5), `FL-57-B1` = c(0.5, 0.5, 0.5, 0, 0.5, 0.5), `FL-57-B2` = c(0.5,
0.5, 0.5, 0, 0.5, 0.5), `FL-57-C2` = c(0.5, 0.5, 0, 0.5,
0.5, 0.5), `FL-57-C3` = c(1, 1, 1, 0, 1, 1), `FL-57-C5` = c(1,
0.5, 0.5, 1, 1, 1), `FL-57-H1` = c(1, 0.5, 0.5, 1, 1, 1),
`FL-57-H4` = c(0.5, 0.5, 0, 0, 0.5, 0.5), `FL-57-H5` = c(0.5,
0.5, 0.5, 0, 0.5, 0.5), `FL-61-1-B1` = c(0.5, 0.5, 0.5, 0,
0.5, 0.5), `FL-61-1-B4` = c(0.5, 0.5, 0.5, 0, 0.5, 0.5),
`FL-61-1-C2` = c(0.5, 0.5, 0, 0, 0.5, 0.5), `FL-61-1-C4` = c(0.5,
0.5, 0.5, 0, 0.5, 0.5), `FL-61-1-H3` = c(0.5, 0.5, 0.5, 0,
0.5, 0.5), `FL-61-1-H4` = c(0.5, 0.5, 0.5, 0, 0.5, 0.5),
`FL-61-1-H5` = c(0.5, 0.5, 0, 0.5, 0.5, 0.5), `FL-62-1-C3` = c(0.5,
0.5, 0, 0.5, 0.5, 0.5), `FL-62-2-H2` = c(0.5, 0.5, 0.5, 0,
0.5, 0.5), `FL-73-H1` = c(0.5, 0.5, 0.5, 0, 0.5, 0.5), P_57_F = c(0.5,
0.5, 0.5, 0, 0.5, 0.5), P_57_M = c(0.5, 0.5, 0.5, 0, 0.5,
0.5)), row.names = c("g1", "g2", "g3", "g4", "g5", "g6"), class = "data.frame")
I tried both on scaled and non-scale values:
fviz_nbclust(scale(tab), kmeans, method = "wss")
fviz_nbclust(tab, kmeans, method = "wss")
and I get this error:
Error in FUNcluster(x, i, ...) :
more cluster centers than distinct data points.
how can I fix it?
Many thanks for your help !
I maybe found the solutions: it was sufficient to specify k.max = any number lower than nrow(tab)

SwiftUI: How to center a rectangle in a list?

How do I center the rectangles in the list? I have tried somethings in my code, with alignment, but it has no affect sadly. Has this something to do with the default behavior of a list?
Here is my code:
struct ContentView: View {
var sectiesList = [
Secties(name: "Algemeen", color: Color.overigPink),
Secties(name: "Natuurkunde", color: Color.natuurkundeBlue),
Secties(name: "Wiskunde", color: Color.wiskundeBrown),
Secties(name: "Scheikunde", color: Color.scheikundeRed),
Secties(name: "Biologie", color: Color.biologieGreen)
]
var body: some View {
NavigationView {
ZStack {
Color.offWhite
List(sectiesList, id: \.name) { secties in
ZStack(alignment: .center) {
RoundedRectangle(cornerRadius: 25)
.fill(secties.color)
.frame(width: UIScreen.screenWidth * 0.85, height: UIScreen.screenHeight * 0.13, alignment: .center)
.shadow(color: Color.black.opacity(0.2), radius: 10, x: 10, y: 10)
.shadow(color: Color.white.opacity(0.7), radius: 10, x: -5, y: -5)
Text(secties.name)
.font(.largeTitle)
.fontWeight(.semibold)
.foregroundColor(Color.white)
.multilineTextAlignment(.center)
}
}
}.navigationBarTitle(Text("Binas"))
}
}
init() {
UITableView.appearance().separatorStyle = .none
UITableViewCell.appearance().backgroundColor = UIColor(red: 225 / 255, green: 225 / 255, blue: 235 / 255, alpha: 1)
UITableView.appearance().backgroundColor = UIColor(red: 225 / 255, green: 225 / 255, blue: 235 / 255, alpha: 1)
}
}
Give ZStack max available space in row, like
ZStack(alignment: .center) {
RoundedRectangle(cornerRadius: 25)
.fill(secties.color)
.frame(width: UIScreen.screenWidth * 0.85, height: UIScreen.screenHeight * 0.13, alignment: .center)
.shadow(color: Color.black.opacity(0.2), radius: 10, x: 10, y: 10)
.shadow(color: Color.white.opacity(0.7), radius: 10, x: -5, y: -5)
Text(secties.name)
.font(.largeTitle)
.fontWeight(.semibold)
.foregroundColor(Color.white)
.multilineTextAlignment(.center)
}
.frame(maxWidth: .infinity) // << here !!

Swift - auto layout UILabel height based on its contents

I am having trouble setting up auto layout for UILabel and would like to get help on this.
In the picture, I have created yellow and green UILabels.
What I want to achieve is dynamically adjust the yellow UILabel height based on its content's number of lines, and anchor top of green box to anchor bottom of yellow box.
Currently, I have code as follow:
// Yellow UILabel
addSubview(captionLabel)
captionLabel.anchor(top: actionButtonsStackView.bottomAnchor, left: leadingAnchor, bottom: nil, right: trailingAnchor, paddingTop: 0, paddingLeft: 8, paddingBottom: 0, paddingRight: 8, width: 0, height: 0)
captionLabel.heightAnchor.constraint(greaterThanOrEqualToConstant: 0).isActive = true
// Green UILabel
addSubview(dateLabel)
dateLabel.anchor(top: captionLabel.bottomAnchor, left: leadingAnchor, bottom: bottomAnchor, right: trailingAnchor, paddingTop: 0, paddingLeft: 8, paddingBottom: 0, paddingRight: 8, width: 0, height: 0)
Note: the anchor function sets top, leading, bottom, and trailing
constraints with respective paddings.
As you can see, the yellow UILabel takes up more space than it actually needs.
If I leave top constraint from green UILabel, the yellow box actually starts to work as I expect:
And the code for this looks like:
// Yellow UILabel
addSubview(captionLabel)
captionLabel.anchor(top: actionButtonsStackView.bottomAnchor, left: leadingAnchor, bottom: nil, right: trailingAnchor, paddingTop: 0, paddingLeft: 8, paddingBottom: 0, paddingRight: 8, width: 0, height: 0)
captionLabel.heightAnchor.constraint(greaterThanOrEqualToConstant: 0).isActive = true
// Green UILabel
addSubview(dateLabel)
dateLabel.anchor(top: nil, left: leadingAnchor, bottom: bottomAnchor, right: trailingAnchor, paddingTop: 0, paddingLeft: 8, paddingBottom: 0, paddingRight: 8, width: 0, height: 0)
What am I doing wrong here?
Actually, removing bottomAnchor from green box make it work:
And the code for this looks like:
// Yellow UILabel
addSubview(captionLabel)
captionLabel.anchor(top: actionButtonsStackView.bottomAnchor, left: leadingAnchor, bottom: nil, right: trailingAnchor, paddingTop: 0, paddingLeft: 8, paddingBottom: 0, paddingRight: 8, width: 0, height: 0)
captionLabel.heightAnchor.constraint(greaterThanOrEqualToConstant: 0).isActive = true
// Green UILabel
addSubview(dateLabel)
dateLabel.anchor(top: captionLabel.bottomAnchor, left: leadingAnchor, bottom: nil, right: trailingAnchor, paddingTop: 0, paddingLeft: 8, paddingBottom: 0, paddingRight: 8, width: 0, height: 0)
But why is it working? I thought by setting greaterThenOrEqual constant, the yellow UILabel auto size itself to just enough size to contain its contents, and the green UILabel adjust its size accordingly to bottomAnchor's yellow UILabel.
That because you're adding two labels which have same priority of vertical content hugging (low by default). If you want the the green label get higher, you can set the Content Hugging Priority value of the red one to high or required. Here the code:
// Yellow UILabel
addSubview(captionLabel)
captionLabel.setContentHuggingPriority(.defaultHigh, for: .vertical)
captionLabel.anchor(top: actionButtonsStackView.bottomAnchor, left: leadingAnchor, bottom: nil, right: trailingAnchor, paddingTop: 0, paddingLeft: 8, paddingBottom: 0, paddingRight: 8, width: 0, height: 0)
// Green UILabel
addSubview(dateLabel)
dateLabel.anchor(top: captionLabel.bottomAnchor, left: leadingAnchor, bottom: bottomAnchor, right: trailingAnchor, paddingTop: 0, paddingLeft: 8, paddingBottom: 0, paddingRight: 8, width: 0, height: 0)
For more details you can this article: https://medium.com/#abhimuralidharan/ios-content-hugging-and-content-compression-resistance-priorities-476fb5828ef

how to fill through color in CGContextStrokeLineSegments?

i am using the code to draw a triangle , but how to fill color in it.
CGContextSetRGBStrokeColor(c, 255, 0, 255, 1);
//CGContextSetRGBStrokeColor(c, 1.0, 1.0, 1.0, 1.0);
// Drawing with a blue fill color
CGContextSetRGBFillColor(c, 0.0, 0.8, 1.0, 1.0);
CGPoint points[6] = { CGPointMake(142, 200), CGPointMake(150, 250),
CGPointMake(150, 250), CGPointMake(135, 250),
CGPointMake(135, 250), CGPointMake(142, 200) };
CGContextStrokeLineSegments(c, points, 6);
First create a path, then fill it:
CGContextSetRGBFillColor(c, 0.0, 0.8, 1.0, 1.0);
CGPoint points[6] = { CGPointMake(142, 200), CGPointMake(150, 250),
CGPointMake(150, 250), CGPointMake(135, 250),
CGPointMake(135, 250), CGPointMake(142, 200) };
CGContextAddLines(c, points, 6);
CGContextClosePath(c);
CGContextFillPath(c);