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
Related
I'm creating a subview to add onto an existing view. I'm trying to assign the background color to this subview to red programmatically but displays as the defaulted color still.
let toggleView = UIView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight))
toggleView.backgroundColor = UIColor.red
As from the above code snippet it looks like that you are creating a view but not adding it as a subview to the parent view. It will be best if you can provide full function so that we can look into the actual cause.
You have created the toggleView with a red background but you have not added it to the main view, this is what you should do:
let screenWidth = view.frame.size.width
let screenHeight = view.frame.size.height
let toggleView = UIView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight))
toggleView.backgroundColor = UIColor.red
view.addSubview(toggleView)
In my Swift app i have inserted an image like this:
let ImageNavig = "5.jpg"
let image1 = UIImage(named: ImageNavig)
let image1View = UIImageView(image: image1!)
let yCenter1 = self.view.center.y
let Image1Size = CGPoint(x: 420, y: 240)
image1View.frame = CGRect(x: 0, y: -10, width: Image1Size.x, height: Image1Size.y)
view.addSubview(image1View)
but, when I run the project with older Iphone the results is this:
how can I insert autolayout programmatically without storyboard?
If you have issue of image width in old iphones then it is because, you have given fix width to the image , use below code it will take width dynamically as per iphone width
let Image1Size = CGPoint(x: self.view.frame.width, y: 240)
instead of
let Image1Size = CGPoint(x: 420, y: 240)
I have a text view and I am attempting to add a line underneath it. I am trying to accomplish this with a CALayer, however it is not showing up in the textView. I would appreciate it if someone could help me. The code is below.
let border = CALayer()
border.backgroundColor = UIColor(hexString: "#CC0000").cgColor
border.frame = CGRect(x: 0, y: messageField.frame.height - 3, width: messageField.frame.width, height: 3)
messageField.layer.addSublayer(border)
In my test app, I just tried this:
let messageField = UITextView(frame: CGRect(x: 30, y: 40, width: view.frame.width - 60, height: 40))
var border = CALayer()
border.backgroundColor = UIColor.red.cgColor
border.frame = CGRect(x: 0, y: messageField.frame.height - 3, width: messageField.frame.width, height: 3)
messageField.layer.addSublayer(border)
view.addSubview(messageField)
And it worked fine. Weird but fine. The color showed up no problem, but it scrolls along with the text view. But that might be what you want. I tried it with different heights for the message field too. I couldn't get it to not show.
If you want it to work and have the line not scroll with the text, try this answer: https://stackoverflow.com/a/38327895/793607
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)
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".