Adding image to UIButton makes it unclickable - swift

I am learning to navigate and use the features of Xcode right now and I don't understand why adding an image to a UIButton through the Attributes Inspector makes the UIButton unclickable. When adding the image, it also resizes the button on the storyboard. I can't seem to find any answers online. Could somebody explain why this behavior occurs?
Before adding the image to the UIButton:
After adding the image to the UIButton:
Edit: and preferably how to fix it :D

The unclickable scenario you described is literally impossible unless you deleted everything in the connections inspector tab. In order for the image not to resize you should use autolayout in the bottom right. Set a constraint on width and height.

Related

UIButton Title not showing inside UIStackView

I created a UIStackView with a UIImageView and a UIButton. The text inside of the UIButton won't show. I can tell that autolayout works because the background and the selection of my UIButton both work properly.
The title always disappears when I have the constraints on. I set the image to 0/0/0/0 without margins and aligned the button to the image's edges.
Has anyone run into this or knows how to fix it?
At the end I simply used a UIView. I don't know what made me use the UIStackView but I guess it's not made for overlaying objects.

I have a UIButton subclass that I have set an image to in IB, and strangely about 1/3 of the button won't respond to touch! What's going on?

As described, I have a UIButton subclass, that I am designing in IB. I have set the button subclass to a UIView, and set an image to the button as well. I have set a UILabel beneath the image, attempting to give it the Finder look. Everything works great, except for the fact that the right 1/3rd of the image won't respond to touch!
It is the strangest thing. The button bounds are set to encapsulate the entire image, but that right 1/3rd won'r respond.
Has anyone seen this before? Does anybody know what's going on?
Thanks

Changed image of UIImageView from code, not centered anymore

So I just spent like one hour Googling for some answers.
I added UIImageView with Interface Builder and I set image to some existing image.
I wanted to make the image centered, so I changed mode to centered. Worked.
Now I wanted to change the image from code. So I set image in IB to nothing and I did imageView.image = (UIImage instance)
Now the image changed, but it's not centered anymore.
Also, here's an interesting thing: I tried also not changing image in IB, I just left it set to some image and then I changed image from code. I could see both images! The one set in IB was centered (like I want it) and the new one was overlaying on the left.
Any ideas please?
Also I probably lack some basic knowledge of this View stuff, can anyone please point me at documentation where this is explained?
It sounds like you have two overlapping UIImageView subviews. One of them is referenced by imageView in your view controller and doesn't center. The other isn't referenced by your view controller but does center. The solution would be to get rid of the non-referenced image view.
I got it! Thanks James Huddleston for useful comments.
You were right. There must have been to UIImageViews.
Here's what happened. I actually used viewWithTag: to find that view (it wasn't linked directly - it was in a UITableViewCell). Well, it apparently didn't work with 0, when I changed tag to 1 - it worked.

UIButton - unwanted background well

I'm creating UIButtons (type = UIButtonTypeCustom) with a custom background drawn by an artist; unfortunately UIButton is adding an unwanted 'well' effect around the backgroundImage (as specified via setBackgroundImage). Is there a way to disable the well? It's not a simple drop shadow, so messing with the CALayer properties doesn't seem to help. I realise I could use UIControl, but that's considerably more work, since I need to handle the label subview myself, and get the artist to produce highlighted versions of the artwork - UIButton is doing all that nicely, if I could only disable the well effect.
Put your image inside a UIImageView, and then position your UIButton on top of it. Use the "custom" style, which has no UI to it at all and is totally invisible.
If you want to change ("highlight") your button image when the button is hit, just change the image contained in the UIImageView in whatever method your UIButton targets.

UIBarButtonItem in middle of navbar?

I have created a flexible navigation bar in my app that will show custom buttons on the left, right, and in the middle. So far I have got working:
Right/Left/Middle - Custom Image and/or Text
Right/Left - Normal looking button with custom Image in it
Right/Left - Normal looking button with custom Text in it
By 'normal looking' I mean the default UIBarButtonItemStyle- just a nice shiny button.
My question is, how can I achieve the same look in the MIDDLE of the nav bar? I can do custom/text images by just creating a custom UIButton, and on the right / left I create a UIBarButtonItem from it, but in the middle I just add the UIButton view as a subview of the navbar.
I can't add a UIBarButtonItem to the navbar as a subview, and any UIButton I create doesn't look like a UIBarButtonItem.
The only workaround I can think of is to use a stretchable custom image that I steal from a screenshot. I'd rather avoid doing it this way. Am I missing something?
*********** UPDATE **************
The open source library Three20 will allow you to create UIButtons that look like UIBarButtonItems. Then you can add them to the bar view either by placing manually as a subview or by setting the title view.
I never understood why they didn't make UIBarButtonItems derive from UIViews (or even better, UIButtons) so they could be used elsewhere.
Sounds like you're on the right track, but need better artwork :-) You can get a large number of iPhone UI components in a Photoshop .PSD file from here. They have pre-rendered bar buttons which you can use as a base for a standard UIButton image. If you have Photoshop (or a decent drawing program) you can stretch the buttons from the middle to fit your size without getting the corners distorted. Just add the label and you should be good to go.
Put a flexible region on the left and right, they will offset each other causing anything between them to be centered