I am relatively new to iPad programming in cocoa touch.
I have a PNG with an alpha channel. I want to overlay it as a subview on a UIButton so that it looks as though the Button has a border around it. (Alpha channel is the centre of the image so the user must still see through it).
I can't seem to find a way to display the alpha channel correctly. The centre still displays as opaque white.
If I can't do that, how would I go about drawing a border around a UIButton? Subclass UIButton and override -drawRect? Keep in mind that the buttons are programmatically (dynamically) added, as well as the borders. (I don't want to remove the button in order to add the border.) The alpha - solution would be preferable because then I can overlay the border image on the superview (scroll view) and just set the offset to correspond to a button.
You can add images or set images to UIButton, but as far as I know you cannot set an alpha mask to the whole thing (if I understand your question correctly). So a standard button wih a hole in it is not possible. But you can use a custom type button and set the Image to whatever image you want, s o this should give you any desired effect. You can add more subviews to it if you need to refine it later.
Edit with overlay:
UIButton *button = [UIButton buttonWithType....]; // your ordinary button here
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"youroverlay.png"]];
// maybe adjust your overlay position here, e.g. imageView.center = .... (use bounds of button, not its frame), or the size directly
[button addSubview:imageView];
Related
I have a UILabel and a UIButton on the UIView (as ORIGIN in screenshot), and then are there any technology to make they gray temporarily? (as EXPECTED in screenshot)
I've try to create a UIVIew (Background color #6666 and alpha 0.75) front of them and get ACTUAL.
https://i.stack.imgur.com/y5xhH.jpg "screenshot"
By the way, the ACTUAL screen shot is captured when presenting a UIAlertController.
Don't set alpha 0.75 instead set 1.0 and you can set title colour for button either in storyboard or xib if you are using storyboard or nib file. You can also set title colour programatically.
[button setTitleColor:[UIColor lightTextColor] forState:UIControlStateNormal];
here button is your button reference and you can set any colour you want.
Edited:
If you have clicked actual image with alert view then you can also set with 0.75 alpha what is best suited you. What only diff i can see is the colour of button text. Which you can set from above mentions methods.
I actually have something simple but I just can't work it out as I'm fairly new to the iOS SDK. So I have a backgroundcolor on an UIView. On top of that I would like to place an image covering the whole UIView as a kind of backgroundimage. The image I'm using is a png which contains transparent areas. I'd like to see the background color through this transparent areas. I do not want to set the transparency of the image itself.
I believe you can set the background color of your view to an image, but this solution should help you learn more about views:
From code (assuming you've added your image to your project):
UIImage *image = [UIImage imageNamed:#"imageFileName"];
UIImageView *imageView = [[UIImageView alloc] initWIthImage:image];
[backgroundView addSubview:imageView];
On storyboard, drag an image view onto your background view. You can configure the image view's frame and image in storyboard. Once you drag the image view into your scene pay attention to where it lands in the view hierarchy. You can choose how exactly you want to set this up, for example, do you want the image view to be a sibling of the background view and lay on top of it? or do you want the image view to be a child of the background view and take up the whole background view's bounds? Same thing as far as the user is concerned, just different organization under the hood.
If you create a transparent area in an image in ex Photoshop, and saving as PNG it will be transparent in Xcode. That's why I always use PNG images.
I created a button on interface builder and set set its "image" to a png file and did not set any background image to it.
I want the button background to be transparent so I set its alpha to 0.6 in IB. The problem is I DO NOT WANT the image to be transparent too ( only the surrounding background ) but the image apparently inherits the button alpha and I can see throughout it.
Is there a way to completely avoid the button image to be transparent ?
Thanks
Update 1:
Using Xcode 5.02
I'm testing it on an iOS6 device, but can also test with iOS7 in the simulator.
Update 2:
Apparently the solution is to rescale the images to match the button rectangle.
The problem is that the button has a title that is positioned below the image to describe the functionality. In this case how could I put the text "floating" above the image ?
I'm assuming you're not programming in iOS 7?
Set the backgroundColor property of your UIButton to [UIColor clearColor]`;
If you've connected an IBOutlet to your UIButton then you can do something like this
[myButton setBackgroundColor:[UIColor clearColor]];
This will only work if you have a well defined transparent image. If you have some areas in your image that is not transparent then the above method will not work and you will simply need to edit your image again to make sure all the areas are actually transparent.
If you're in iOS 7, I believe the UIButton is already transparent and its up to your image to do the rest.
UPDATE 1
OP has requested for the title to remain above the image of the UIButton. Here is the solution for that,
What you need to do is set the backgroundImage property of the UIButton instead of simply setting the image of the button which will push the title down as you have found out.
Again I'm assuming you have an IBOutlet connected to your button; simply do the following:
Make sure you delete any image setting code in interface builder so that the properties are blank.
Then implement the code below
//This is the bit that does the magic - setting the background property
[myButton setBackgroundImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
//Then you can set the title as you require, the title will be centered floating above your background image.
[myButton setTitle:#"Localised String" forState:UIControlStateNormal];
[myButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
And you will get something like this:
If you are using a proper PNG file with transparency in the file, this works automatically without changing the alpha value. If the PNG does not have the transparent area defined properly you will not be able to achieve this by a property (only by complicated drawing methods, which is probably not want you want).
You have to use a PNG image with transparent background. To remove background colour of a image, Let me suggest you a free tool "GIMP" I've been using it for a while.
As already been said - you can use semi-transparent PNG for image.
As for putting up title over the image - you would need to write some code.
You won't be able to do this in Interface Builder.
So you can easy create button
#interface MyButton : UIButton
#end
#implementation MyButton
- (void)layoutSubviews {
[super layoutSubviews];
[self bringSubviewToFront:self.titleLabel];
}
#end
As for image, covering the whole area - it's better, when possible, to use background image rather image.
For partial background color transparency - you always can set semi-transparent background color for it in IB or in code:
[button setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.5]];
Is drag and drop of round rect button no longer available in Xcode 5? I can't seem to find it in the Interface Builder. I was guessing that this is one of the changes in iOS 7, but I just wanted to make sure.
Can also make the rounded rect within the storyboard.
The Round Rect button from Xcode 4 and previous versions appears to have been replaced with the System Default button, which also happens to be clear. I.e. by default there is no white background with rounded corners, only the text is visible.
To make the button white (or any other colour), select the attributes inspector and scroll down to the View section:
Select Background and change it to White:
If you want rounded corners you can do so with a little bit of code.
Just ctrl-drag from the button to your .h file, call it something like roundedButton and add this in your viewDidLoad:
CALayer *btnLayer = [roundedButton layer];
[btnLayer setMasksToBounds:YES];
[btnLayer setCornerRadius:5.0f];
This was too long for a comment in #Robert's answer, but I just wanted to add regarding the statement: "The Round Rect button from Xcode 4...appears to have been replaced...".
Confirming that it definitely has been replaced:
The rounded rectangle button is deprecated in iOS 7. Instead, use a
system button—that is, a UIButton object of type UIButtonTypeSystem.
iOS 7 system buttons don’t include a bezel or a background appearance.
A system button can contain a graphical symbol or a text title, and it
can specify a tint color or receive its parent’s color.
...
If you need to display a button that includes a bezel, use a button of
type UIButtonTypeCustom and supply a custom background image.
Apple iOS 7 Transition Guide, p. 45, "Rounded Rectangle Button"
So, Apple's recommendation is to use a background image.
Actually in ios 7 the total UI has been changed for basic controls.
If still you want to have a rounded rect button, you have to go for the coregraphical property of UIView subclasses i.e; layer property.
buttonObj.layer.cornerRadius = 5.0f;//any float value
to show the effect you have to give some background color to buttonObj
if you want to set the border width
buttonObj.layer.borderWidth = 2.0f;//any float value
you can also give the color for border
buttonObj.layer.borderColor = [[UIColor greenColor]CGColor];
Note: Here we have to use CGColor for the layers because layers are the core graphical properties of UIViews
Unfortunately it is not completely clear what I am trying to do from the title. I want to create an interactive tutor for my IOS application. At the moment I have a simple UIviewcroller with some images, but I want to make it more interactive. I want to add ontop of my normal UIview a second UIview which is partially transparent to enable the user to see what there is below it, and in some parts, invisible. there are the parts that the user has to touch. on this UIview there will be arrows, labels, and other information. Is this possible? If a UIview is covered by another, can I make it possible for the user to interact with parts of the one below it?
Set view.userInteractionEnabled = NO; to disable interaction.
And view.backgroundColor = [UIColor clearColor]; for a transparent background.
Touches will go right through the view to the next one below it.
EDIT:
Alternatively you could try setting view.alpha = 0.0f;. This will make the view transparent. Then you add any buttons and labels to this view. These buttons will be visible and work as expected, but tapping anywhere else will pass through the transparent view. If alpha is below some threshold (not sure about the exact value), touches are ignored and passed to the next view.
On the subview you want transparent set userInteractionEnabled = FALSE. You can also do the same thing with UIWebVIew, UIImageView, etc. also you can set the background color to UIColor clear color. That way it's see through to the layers below.
Try making top view transparent and add a third view below and make its bg-color black and set alpha 0.5