insertSubview() covers the whole Mainview even though inserted at 0 - swift

I insert a UIView into a UIButton with following code:
let backgroundView = UIView()
backgroundView.backgroundColor = .red
backgroundView.translatesAutoresizingMaskIntoConstraints = false
blueButton.insertSubview(backgroundView, at: 0)
NSLayoutConstraint.activate([
backgroundView.leadingAnchor.constraint(equalTo: blueButton.leadingAnchor),
backgroundView.trailingAnchor.constraint(equalTo: blueButton.trailingAnchor),
backgroundView.topAnchor.constraint(equalTo: blueButton.topAnchor),
backgroundView.bottomAnchor.constraint(equalTo: blueButton.bottomAnchor)
])
The promlem is that the backgroundView covers the whole button and I only see the red field instead of the button (its an image) with a red background (my subView).
This is the storyboard part of my image. I don´t do something else in the code with this buttons with the exception of trying to put a subView into:
My goal is to have a circle background like in the picture below. For example the red circle button has a rounded red (but brighter than the buttons red) backgroundView.The standard background of the button should stay the same.
I also tried to send the subView into the background but it didn´t work for me:
blueButton.sendSubviewToBack(backgroundView)
I reduced the alpha value of the subview to see if the button image is still there.
for view in blueButton.subviews {
view.alpha = 0.5
}
And it is:
How can I solve this problem?

Do not inject extra subviews into a button. If the goal is to make the background color of the button red, set its backgroundColor to .red. If the goal is to put something behind the button image, that is what the backgroundImage is for. Or just make the button image look like the image you really want.
I made this button:
It looks a lot like your picture. Here's the code:
let im = UIGraphicsImageRenderer(size: CGSize(width: 30, height: 30)).image {
ctx in
UIColor.red.withAlphaComponent(0.2).setFill()
ctx.cgContext.fillEllipse(in: CGRect(x: 0, y: 0, width: 30, height: 30))
UIColor.red.setFill()
ctx.cgContext.fillEllipse(in: CGRect(x: 5, y: 5, width: 20, height: 20))
}
b.setImage(im.withRenderingMode(.alwaysOriginal), for: .normal)
I think that's a lot better than messing with unwanted illegal subviews.

Related

Button with multiple accessible labels and images in Swift

I need to create custom button class, reuse it 4 times and I also need to override its text and image name. My next problem is how to set its frame somehow dynamically (now it is static), because I need this 4 buttons in grid 2x2.
I'm trying to create button exactly like this: https://imgur.com/a/dNhUGhc.
I have coded this but it is static and in ViewController I can't edit (override) these labels and image name. And if I tried to reuse this class I would have them in the same spot, because frame settings is exactly the same.
I'm subclassing UIButton. If something more suitable exists just let me know.
Code for adding label
// city label
let cityRect = CGRect(x: 0, y: 20, width: buttonWidth, height: 25)
let cityLabel = UILabel(frame: cityRect)
cityLabel.text = "Label"
cityLabel.font = UIFont.systemFont(ofSize: 17, weight: .semibold)
cityLabel.textAlignment = .center
addSubview(cityLabel)
Code for adding image
// image
let imageView = UIImageView(image: UIImage(named: "something"))
imageView.frame = CGRect(x: 0, y: 60, width: 40, height: 40)
imageView.center.x = self.center.x - 20
addSubview(imageView)
Can you guys help me? Thanks
It looks like what you need to do is use an IBOutlet. Basically, an IBOutlet will give you a reference within your code (custom UIView or UIViewController subclass) to the button that you've setup in xib or storyboard. Then you can make any changes or adjustments that you want to it at runtime.
Check this out to learn more about IBOutlets and how to set them up in your project.

swift create a dot on UIimage

I need your help with solving this problem.
My UIImage is allowed to be moved around the screen using PanGesture and it also has TapGesture to rotate with trasform
.transform.rotated(by: CGFloat(M_PI_4))
After user is done with positioning and rotating, he clicks the UIbutton to show a four dots in corners of the Image
//dotcode
let dotSize = 20
let firstDotView = UIView()
firstDotView.frame = CGRect(x: 0, y: 0, width: dotSize, height: dotSize)
firstDotView.backgroundColor = UIColor.black
firstDotView.layer.cornerRadius = 10
view.addSubview(firstDotView)
1: dots like that, don't have any connection with green space
Dots always show in the corner

Changing the separator color of UINavigationBar

I’m working on a iOS-App based on Xcode’s “Master-Detail Template” and want to use custom colors for some of the UI elements.
However, I couldn’t find out how to change the right separator of the UINavigationBar:
I’ve already tried to change the backgroundColor of UINavigationBar, UINavigationItem and its titleView but without success.
Would be great if someone has a clue.
EDIT:
I’ve just noted that viewed in vertical mode, it’s the whole separator that I want to ink?
A slight modification of this answer gives you
extension UINavigationBar {
func setRightBorderColor(color: UIColor, width: CGFloat) {
let rightBorderRect = CGRect(x: frame.width, y: 0, width: width, height: frame.height)
let rightBorderView = UIView(frame: rightBorderRect)
rightBorderView.backgroundColor = color
addSubview(rightBorderView)
}
}

Animation text does not appear in my UIView object

I am using a UIObject to display an animation using Lottie framework which is a submit button, and when this UIView object is tapped, the animation is played.
However the text within this animation does not appear, and the dimensions are also wrong. My UIView object has dimensions of 193*33 and the animation also does. Here is my code;
let SubmitButton = LOTAnimationView(name: "submit_195_33")
self.SubmitViewName.addSubview(SubmitButton)
SubmitButton.frame = CGRect(x: 0, y: 0, width: 195, height: 33)
SubmitButton.contentMode = .scaleAspectFill
SubmitButton.play()
My UIView object is larger then the animation even if I use this code, and the animation is super small like this:
Maybe you can try setting constraints to the button, for example:
submitButton.translatesAutoresizingMaskIntoConstraints = false
submitButton.widthAnchor.constraint(equalToConstant: 195).isActive = true
submitButton.heightAnchor.constraint(equalToConstant: 33).isActive = true

Positioning a NSImage within a NSButton

I have an NSButton, created programatically. within said button, I have an NSImage that serves as an icon:
let button = NSButton(frame: NSRect(x: 10, y: (200 + (50 * id)), width: 100, height: 50))
button.action = #selector(self.switchToView)
let img = NSImage(named: "wob_icon")
button.image = img
What I'm trying to do is get the image 10pt. from the left side of the button, while centred vertically. So far, the image shows up centered horizontally as well as vertically, but since it doesn't seem like I'm able to define a frame or something like that, I can't really move it.
Is there a way to move the image within it's parent (the button)?
Thanks.
Maybe you could use the imagePosition property on a NSButton? (documented here).
It uses an enum of type NSCellImagePosition (documented here) and allows you to set the image to the left of the text, to the right of the text, above the text, below the text and so on.
You still won't have the opportunity to align things pixel perfect but if you can live with that, then imagePosition seems like the way to go.
Here is how to use it:
let button = NSButton(frame: NSRect(x: 10, y: 10, width: 100, height: 50))
button.action = #selector(self.switchToView)
let img = NSImage(named: "wob_icon")
button.image = img
button.imagePosition = .imageLeft
button.title = "Look left :)"
And that gives me this stunning UI:
Hope that helps you.