Swift: NSStackView, bug only one subview is added - swift

Hello in my code below I want to add NSImageView to my stackView, but there is a bug because there is only one that is added. The loop is 3 iterations so normally I should have 3 images:
let imageView = NSImageView(frame: NSRect(x: 0, y: 0, width: 50, height: 50))
imageView.image = image.image
icons.forEach { _ in
stackImage.addArrangedSubview(imageView)
}
print(stackImage.subviews.count) // Outpout 1

Create the NSImageView instances inside the forloop.
And you need to check stackImage.arrangedSubviews.count not stackImage.subviews.count
var icons = [NSImage(named: ""),NSImage(named: ""),NSImage(named: "")]
icons.forEach { image in
let imageView = NSImageView(frame: NSRect(x: 0, y: 0, width: 50, height: 50))
imageView.image = image
stackImage.addArrangedSubview(imageView)
}
print(stackImage.arrangedSubviews.count)

Related

Trying to change the frame of UIView in swift whilst the app is running?

I am trying to change the frame of a UIView when the orientation of my device is changed but nothing appears to work. Here is my code:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
guard
let portraitScreenView = viewScreenSizePortrait,
let overLay = overLayView else{return}
if UIDevice.current.orientation.isPortrait{
print("isPortrait")
overLay.frame = CGRect.init(x: 0, y: 0, width: portraitScreenView.width, height: portraitScreenView.height)
self.overLayView.frame = overLay.frame
}else{
print("isLandscape")
overLay.frame = CGRect.init(x: 0, y: 0, width: portraitScreenView.height, height: portraitScreenView.width)
self.overLayView.frame = overLay.frame
}
print("self.overLayView.frame: \(self.overLayView.frame)")
}
Add self.setNeedsDisplay() after your frame change.

Swift UIView "Empty Image"

Could someone explain me the difference between these two pictures please?
Code with preview of the UIView:
Code without the preview of the UIView:
What's the difference? And why can't I get a preview on the second code example? It only shows "empty image"....
Thanks for helping me.
This seems to be a "feature" (bug) of a Swift playground. If you don't create the view instance using a non-zero frame width and height, you will get "empty image".
This works:
let rect = UIView(frame: CGRect(x: 0, y: 0, width: 1, height: 1))
rect.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
rect.backgroundColor = .green
But this doesn't:
let rect = UIView(frame: .zero)
// also bad: let rect = UIView(frame: CGRect(x: 0, y: 0, width: 1, height: 0))
rect.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
rect.backgroundColor = .green
And remember that:
let rect = UIView()
is essentially the same as doing:
let rect = UIView(frame: .zero)
So when using a playground, create a view with a non-zero frame width and height in the initializer if you don't want to see "empty image".

Add subView behind items to UINavigationBar in iOS 11

In my UINavigationBar subclass I am adding a subView with insertSubview(customView, at: 0) in order to place it in the very 'back' of the UINavigationBar.
In iOS 10 the added subView will be added the Title and UIBarButtonItems as I want it, but in in iOS 11 it seems like Apple changed something because no matter what I try the customView will be placed every time in the front of the UINavigationBar.
//containerView
containerView.frame = CGRect(x: 0, y: -(statusBarHeight), width: UIScreen.main.bounds.width, height: frame.height + statusBarHeight)
containerView.clipsToBounds = true
insertSubview(containerView, at: 0)
I already tried changing the index.
Any idea how I should tackle that problem?
You can look at this. I solved it this way:
override func layoutSubviews() {
super.layoutSubviews()
for aView in self.subviews {
if NSStringFromClass(type(of: aView)) == "_UINavigationBarContentView" {
aView.frame = CGRect(x: 0, y: 20, width: aView.frame.width, height: aView.frame.height)
}
else if NSStringFromClass(type(of: aView)) == "_UIBarBackground" {
aView.frame = CGRect(x: 0, y: 0, width: aView.frame.width, height: self.frame.height)
}
}
}

Transparency when drawing in CGContext does not work Swift

Hello i'm developing a drawing application. I try to draw multiple lines with transparency in a context (one line over another) but the transparencies do not work as I thought they work when drawing one image over another. I've made this test with 2 squares:
var imageView:UIImageView = UIImageView()
imageView.frame = CGRect(x: 0, y: 0, width: 400, height: 200)
self.addSubview(imageView)
/* CREATE A SQUARE UIImage */
let color:UIColor = UIColor(red:1.00, green:0.40, blue:0.21, alpha:1.0)
let bounds:CGRect = CGRect(x: 0, y: 0, width: 200, height: 200)
UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0.0)
var ctx = UIGraphicsGetCurrentContext()
ctx?.setFillColor(color.cgColor)
ctx?.fill(bounds)
let rectangleUIImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
/* DRAW SCUARE WHIT ALPHA 1 */
UIGraphicsBeginImageContextWithOptions(imageView.frame.size, false, 0.0)
ctx = UIGraphicsGetCurrentContext()
ctx?.setAlpha(1.0)
ctx?.draw(rectangleUIImage.cgImage!, in: CGRect(x: 0, y: 0, width: 200, height: 200))
/* DRAW 10 SQUARES WHIT ALPHA 0.1 IN THE SAME PLACE */
ctx?.setAlpha(0.1)
for i in 0..<10 {
ctx?.draw(rectangleUIImage.cgImage!, in: CGRect(x: 200, y: 0, width: 200, height: 200))
}
imageView.image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
I would expect the above code to result in this:Two boxes visually with the same transparency
But no matter how many times I draw a transparent square on another I always find this :( :
Two boxes, one without transparency and one with transparency

Issue with UITextField's leftView property

I am trying to add a single blank UIView to multiple UITextField in order to give a little padding. But my app freeze (does not crash). Can anyone tell me what the issue might be? Here is my code.
var paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 50))
It works well when I assign this view to single UITextField
emailTextField.leftView = paddingView
emailTextField.leftViewMode = UITextFieldViewMode.Always
But App does not responds if I add same paddingView to another UITextField e.g
someTextField.leftView = paddingView
someTextField.leftViewMode = UITextFieldViewMode.Always
My Environment is xCode 6.4, Swift 2.0.
So, I was having and issue with the same exact problem. My solution was to create a paddingView for each textfield.
var paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 50))
var paddingView2 = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 50))
emailTextField.leftView = paddingView
emailTextField.leftViewMode = UITextFieldViewMode.Always
someTextField.leftView = paddingView2
someTextField.leftViewMode = UITextFieldViewMode.Always