Hey I want to create a checkbox in the screen, without using two different images with V and without. Does anyone know how I can do it? I didn't find UI class for it.
In your case, you can use a UIButton. Therefore having the V image will suffice. (Empty square image method is also explained)
The main method you need to call is:
//Set the 'V' image for UIControlStateSelected and [UIImage new] for UIControlStateNormal
- (void)setImage:(UIImage *)image
forState:(UIControlState)state;
//Then, for the surrounding square box you can either use an image, OR better, use the borderWidth and borderColor property of the button's layer to achieve this effect.
//Image Method
- (void)setBackgroundImage:(UIImage *)image
forState:(UIControlState)state; // Incase you use an image then, use the same image for both UIControlStateSelected and UIControlStateNormal.
//drawing method
[checkBoxButton.layer setBorderWidth:<Enter Width in CGFloat>];
[checkBoxButton.layer setBorderColor:<Enter Width in CGColor>];
NOTE: Here the crucial piece will be to maintain the selected state of the button by toggling the 'selected' property. The rest will take care of itself.
That involves manually creating a UICheckbox which draws a 2D rectangle view and a custom view for the check.
So you will have to create your own UI element.
Related
I have UIButtons that I was trying to set the images to aspectFit. However using Storyboards I ran into some difficulty I subclassed UIButton and programatically set the imageViews of the buttons to ScaleAspectFit
#import "UIButtonWithImageAspectFit.h"
#implementation UIButtonWithImageAspectFit
-(void)awakeFromNib{
[self.imageView setContentMode:UIViewContentModeScaleAspectFit];
}
#end
This worked well and the buttons images display nicely.
However, now I have a need to swap out the images of the button programatically so I tried:
[_firstLetterButton setImage:[UIImage imageNamed:#"2CeadLitir.png"]];
But this does not work so according to advise here I must use:
[_firstLetterButton setBackgroundImage:[UIImage imageNamed:#"2CeadLitir.png"] forState:UIControlStateNormal];
which adds the image as a backgroundImage to the button. Now I have to images in the button.
that is scaled nicely as an image property of the button,
and another that is not scaled nicely as a background image displayed behind.
So I returned to the subclass of UIButton UIButtonWithImageAspectFit
and tried to change it so that it sets the contentMode of the backgroundImage rather than the image
But it does not seem to have a backgroundImage property that I can access but according to the documentation it does have
currentBackgroundImage
Property
However I cannot use the same method setContentMode on that currentBackgroundImage
Any ideas how to get around this?
You need to consider this illustration from Apple web-site.
It's from Buttons tutorial. In particular this:
State
A button has four states to configure for appearance—default, highlighted, selected, and disabled.
And form
UIButton class reference
Discussion
In general, if a property is not specified for a state, the default is to use the UIControlStateNormal value. If the UIControlStateNormal value is not set, then the property defaults to a system value. Therefore, at a minimum, you should set the value for the normal state.
And here is information about different UIControlState:
All this links is must-read for beginner iOS Developer.
Actually the UIButton has a method
- (void)setImage:(UIImage *)image
forState:(UIControlState)state
so All I needed to do was set the state when setting the image on the UIButton instead of on the UIButton.imageView
I've an image , I want to increase/decrease its brightness by using a UISlider? How can I do it?
Is there any default method are available for this? If yes, please tell me..
Thanks!
If you don't want to use the link and want to code a custom solution try this:
In your Storyboard add an imageView with the image
Add a view that has a white background color
Add a view with a black background color
Add a slider and make sure they aren't in each others views!
Now use CNTRL + click to create your properties in the .M file
Create the method for the slider by CNTRL + clicking below the viewDidLoad method and name the method something like sliderChanged:
Change (id)sender to (UISlider *)sender
Last thing to do, fill the method body with this!
these are screenshots from the custom brightness app you just made:
I'm creating UIItableview. For the first cell I want to have a user Image with a button to allow him to change the image. (not so complicated).
Somehow I have a strange problem: When I set setMasksToBounds:NO the [self.userTable.layer setCornerRadius:20.0]; is simply get disabled.
I am attaching a code with output example:
//Initiate UserTable
//[self.userTable setClipsToBounds:NO];//from the documentation this line is set by default
[self.userTable.layer setCornerRadius:20.0];
[self.view addSubview:self.userTable];
later in cellForRowAtIndexPath: I add:
[myCellView.contentView addSubview:photo]; //My photo is UImageview
If I uncomment the line [self.userTable setClipsToBounds:NO] I get:
but my corners are not rounded any more......
I read the following questions
this and this and still cant figure that out?
Did someone had this problem and can provide a fix which will not force me to change all my code? and also how the 2 options are related to each other?
Thanks a lot.
Calling setCornerRadius applies a mask to your CALayer, which is then used to mask the bounds and create the rounded corner effect.
By calling setMasksToBounds:NO, you are disabling the mechanism used to round the corners.
In short, you can't have it both ways.
Consider making that row in the tableview taller, or using a custom view instead of a standard UITableViewCell.
I'm trying to use my own images on a C4Stepper. I'm setting it up like this
#implementation C4WorkSpace{
C4Stepper *theStepper;
C4Image *stepperBackground;
C4Image *stepperPlus;
}
-(void)setup {
//load backgroundimage
stepperBackground=[C4Image imageNamed:#"icon_zoom.png"];
stepperBackground.width=100;
//load incrementImage
stepperPlus=[C4Image imageNamed:#"icon_zoom_plus.png"];
stepperPlus.width=stepperBackground.width/2;
//setup stepper
theStepper=[C4Stepper stepper];
[theStepper setBackgroundImage:stepperBackground forState:NORMAL];
[theStepper setIncrementImage:stepperPlus forState:NORMAL];
theStepper.center=self.canvas.center;
[self.canvas addSubview:theStepper];
}
The 2 icons look like this:
Strangely, what appears on the canvas looks something like this: !
Are there any special requirements for the zoomStepperImages? A certain size or something?
There are special requirements.
The background image used is "stretchable", so you have to design a button image that can be stretched if needed. If you have a look at any of the button images (incl. in the Media.xcassets folder) you'll see that they are generic. For example, the selected button background image looks like:
Similarly, the increment / decrement images are themselves independent of the background images. The incrementNormal image included in the same folder looks like:
You can see how the default values of the C4Stepper class are initialized in C4AppDelegate.m:
...
[C4Stepper defaultStyle].style = basicStyle;
[C4Stepper defaultStyle].tintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"lightGrayPattern"]];
[[C4Stepper defaultStyle] setDecrementImage:[C4Image imageNamed:#"decrementDisabled"] forState:DISABLED];
[[C4Stepper defaultStyle] setDecrementImage:[C4Image imageNamed:#"decrementNormal"] forState:NORMAL];
[[C4Stepper defaultStyle] setIncrementImage:[C4Image imageNamed:#"incrementDisabled"] forState:DISABLED];
[[C4Stepper defaultStyle] setIncrementImage:[C4Image imageNamed:#"incrementNormal"] forState:NORMAL];
So, what you want to do is create an image for your background (mine is stretchable because the stuff in the middle are straight lines that can be stretched horizontally without looking stretched. You also want to create independent + - images for increment / decrement sides of the button.
Just as a note, when I was creating the C4Button class, I had to play around with the various images I was creating to get them to "look" right... In the end, I had to do a lot of tweaking and experimenting to learn how they were rendered. The difficulty I had in figuring this out was that the rendering mechanism of UIButton itself was a bit strange and I had to learn how to fit things into the button so that they rendered properly.
I added two UIBarButtonItems to a toolbar.
The first I added using
initWithImage:style:target:action:
For the second, I created a UIButton, containing a UIImageView with the image set to the same used in the first example. I also made the image view slightly smaller that the button to allow room for a label. Then I created the BarButtonItem with
initWithCustomView:
I get very different renderings of the image:
What is going on here, and how do I get the image to be displayed as expected in the second example?
Note: I actually added the buttons in reverse order - in the image above, the first button is using initWithCustomView:
According to the documentation for initWithImage:style:target:action
The images displayed on the bar are derived from this image [...] The alpha values in the source image are used to create the images—opaque values are ignored.
So, the image is being used as a mask. This is the behavior you typically see used in toolbars, but is available here as well. I guess you need to use custom views if you want the image pixels to be used on the bar.