I want to make use of the UIStepper but it's too small for my needs. That's why I found out that
[UIStepper setBackgroundImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
Gets the stepper as big as the picture is. The problem here is that it sets the background for both "tiles", the + and the - one.
Is there a possibility to change the background of the individual tiles?
Alternatively I could just make 2 buttons work the same way as a stepper does, but just continuing with the stepper would save me 30 minutes.
Ok I had a quick look into the .h file and found:
- (void)setDividerImage:(UIImage*)image forLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;
- (UIImage*)dividerImageForLeftSegmentState:(UIControlState)state rightSegmentState:(UIControlState)state NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;
Which means you can pretty much do your own stile as you can put together the images of the left and the right tile in one file + set a custom divider.
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'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 Am trying to create a UIBarButton as a flat Round Rect. sort of a mix between iOS 7 and 6.
Heres an example from the Facebook app
I want a Button that looks like the "send" button.
Anyone has any idea?
Im actually surprised that Apple don't give this option for us while they use it in quite a few places. sometimes just writing in colour is not enough to make the user understand its a button.
If you were to put a UIButton inside a UIBarButtonItem and set the backgroundColor, then you could use the cornerRadius property of it's layer:
self.button.layer.cornerRadius = 5;
In order for this to work, you need the CoreGraphics.framework and #import <QuartzCore/QuartzCore.h>
You can just provide a custom button background image with rounded corners and set it as the background image inside the storyboard.
Im making a game of sorts, where balls are added to the screen. I need to be able to add custom balls (different speeds/images, ect) at time intervals (i.e. ball type 1 gets added every 15 seconds, whereas ball type 2 gets added every 30 seconds). I need these balls to be UIImageView (at least with the setup I have right now to check collisions).
Any ideas on how I can do this?
Also, How to make the player (a UIImageView) the start button. I have an IBAction that starts the game. I cannot connect the UIImageView and the IBAction though...
Only a partial answer first, but really you don't want to use UIImageView for user interaction. A UIButton can be set to the type 'custom' and then display an image (you can set the image in interface builder or programmatically):
btnImage = [UIImage imageNamed:#"image.png"];
[btnMyButton setImage:btnImage forState:UIControlStateNormal];
It is then much easier to pick up IBAction etc.
As regards creating the balls dynamically, creating a new UIImageView programmatically is easy using the initWithImage method, I suggest you read up on the UIImageView class reference at http://developer.apple.com/library/ios/ipad/#documentation/uikit/reference/UIImageView_Class/Reference/Reference.html.
UIImageView inherits from UIView so after creating each image, just add it as a sub view using something like:
[self.view addSubView:newImageView];
Regarding the timer element, NSTimer is the place to look for this. I don't have much experience using these but it should be straightforward. Reference can be found at:
http://developer.apple.com/library/mac/ipad/#documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/Reference/NSTimer.html
Hth
I want to know whether drawing a round UIButton(not rounded rect) is possible.
When I add a round image in a UIButton of type custom, then it looks like a round button. But at the moment the button is clicked the boundary of the button becomes visible, so that it looks like a square button, then again when the click ends it looks like a round button.
I want the button to look like a round button even at the moment the click happens. is this possible?
Tested Code:
.h
-(void)vijayWithApple;
.m
-(void)vijayWithApple{
NSLog(#"vijayWithApple Called");
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"Micky.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(vijayWithApple) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(135.0, 180.0, 40.0, 40.0);//width and height should be same value
button.clipsToBounds = YES;
button.layer.cornerRadius = 20;//half of the width
button.layer.borderColor=[UIColor redColor].CGColor;
button.layer.borderWidth=2.0f;
[self.view addSubview:button];
Result
Just create two rounded button images, one for the pressed state and one for unpressed.
In IB, create a UIButton of type Custom. Go to the section where you set a background image, and set the dropdown with "All" to normal - now set the image to your unpressed image.
Then change the dropdown to "Selected", and put in your pressed image.
Now change the fill type to be aspect fit, and make the button sqare. Use as a normal UIButton anywhere. You can of course also easily do this all programaitcally with similar steps (setting images for UIControlStateNormal and UIControlStateSelected).
You'd probably have to create a custom control for that, but do you really need a round button?
Every platform has its UI conventions, and the iPhone is no exception. Users expect the buttons to be rounded rectangles.
UPDATE in response to comment:
If I'm getting this right, you're not looking for a round button, but rather a clickable (touchable) image.
You can use an UIImageView and its touchesBegan method.
UPDATE 2:
Wait a second. What kind of radio button are we talking about? For some reason I thought you were trying to imitate a real radio. If you're talking about a radio button group, please use a UISegmentedControl or a UIPicker.
UIButton *but=[UIButton buttonWithType:UIButtonTypeCustom];
but.layer.cornerRadius=50;
From what I know of the SDK (admittedly I have very little experience beyond tutorials in books/screencasts), you'd have to create your own control to have a radio button. There are a couple of suggestions on how to implement that in this question.
That said, from a user's perspective I feel like it would be better to stick to the native controls. iPhone users are used to specific controls (i.e. UITableViews or UIPickerViews) for this type of interaction. Straying from practically universal UI conventions tends to make the user experience more jarring.
take the help of this site for creating Round Button on iPhone.
You can get many custom controls :)
http://www.cocoacontrols.com/controls
http://www.cocoacontrols.com/platforms/ios/controls/uiglossybutton
H#PPY CODING !
Thanks & regards,
Gautam Tiwari