I can't seem to change the size of UIStepper:
In IB, the Width and Height boxes are grayed out.
I used initWithFrame:
UIStepper *stepper = [[UIStepper alloc] initWithFrame:CGRectMake(300, 638, 120, 80)];
But it does not change the size. Several posts on SO seemed to implied it is changeable. Any suggestion?
UIStepper* s = [UIStepper alloc] init];
s.transform = CGAffineTransformMakeScale(0.75, 0.75);
You can properly update the UIStepper size without transformation.
Use the following method to set the background image and the stepper will draw itself using the background size:
- (void)setBackgroundImage:(UIImage*)image forState:(UIControlState)state
Example
[self.stepper1 setIncrementImage:[UIImage imageNamed:#"plusIcon1.png"] forState:UIControlStateNormal];
[self.stepper1 setDecrementImage:[UIImage imageNamed:#"minusIcon1.png"] forState:UIControlStateNormal];
[self.stepper1 setBackgroundImage:[UIImage imageNamed:#"stepperBkg1.png"] forState:UIControlStateNormal];
[self.stepper1 setBackgroundImage:[UIImage imageNamed:#"stepperBkgHighlighted1.png"] forState:UIControlStateHighlighted];
[self.stepper1 setBackgroundImage:[UIImage imageNamed:#"stepperBkgDisabled1.png"] forState:UIControlStateDisabled];
This yields the following result on the left, compared to an unmodified stepper on the right:
stepperBkg1#2x.png:
stepperBkgHighlighted1#2x.png:
I tried the transform on my stepper - it did change the appearance and did scale it, however, the images of the + and - were stretched (so you have to scale in proportion to the original stepper.
Also, be careful because the area of touch that actually increments and decrements, does change - so on the stretched image, the button would not decrement along the entire view - so this is probably not a good solution....
from the doc:
The bounding rectangle for a stepper matches that of a UISwitch object.
Doesn't sound, like it is possible upfront.
Also in this blog post:
// Frame defines location, size values are ignored
UIStepper *stepper = [[UIStepper alloc] initWithFrame:CGRectMake(120, 20, 0, 0)];
But you can try to transform it's layer.
You can provably scale it:
stepper.transform = CGAffineTransformMakeScale(1.75, 1.0);
I've made a small custom UIStepper class for it. No images needed, no transformation needed. Images generated automatically.
https://github.com/alelipona/VZCustomSizeStepper
Yes, you can change size of stepper.
first, right click on storyboard --> select (open as)--> Select (Source Code)
then find stepper in the code--> find width=??? and change.
then click on storyboard again and select open as interface builder.
Related
here is my code but it covering my total slider frame i want to change only the slider icon it is not working
[mySlider setThumbImage: [UIImage imageNamed:#"slider.png"]forState:UIControlStateNormal];
mySlider.minimumValue = 2000;
mySlider.maximumValue = 4000;
mySlider.continuous = NO;
[mySlider addTarget:self action:#selector(sliderChanged:)
forControlEvents:UIControlEventValueChanged];
You need to set the initial thumb image using setThumbImage forState. This works fine for me in iOS 10 and returns the correct width of the image. The slider is setup in a storyboard and connected via an IBOutlet.
Looking at the properties that UISlider provides, you can get a pretty strong sense that while the class is designed to let you customize it's appearance, dynamically changing the appearance of the control as the user adjusts it probably wasn't what the designer had in mind. Otherwise, they'd have included the sort of mechanism you suggest: a means to associate different images with ranges of values.
self.thumbImage = [self imageWithImage:[UIImage imageNamed:#"IconForSlider.png"] scaledToSize:CGSizeMake(frame.size.height, frame.size.height)];
size = self.thumbImage.size.height;
[self setThumbImage:[self imageRotatedByDegrees:0] forState:UIControlStateNormal];
[self addTarget:self action:#selector(changeValue) forControlEvents:UIControlEventValueChanged];
I cannot find a way to set the height of a NSProgressIndicator programmatically.
My try so far:
NSProgressIndicator *ind = [[NSProgressIndicator alloc] init];
[ind setStyle: NSProgressIndicatorBarStyle];
// Height does not change height of the actual indicator
[ind setFrame: NSMakeRect(0, 0, 100, 50)];
[ind setBounds: NSMakeRect(0, 0, 100, 50)];
//[ind setControlSize: 0]; does only make it smaller, not bigger
[view addSubview: ind];
I found NSProgressIndicatorBarStyle enumeration in the documentation, but I couldn't find a method to specify the thickness.
Here a screenshot describing my problem: (layer has a background of red for better understanding):
This also occurs when using the NSButton class. Is there a workaround for this?
in iOS You can't change the progress indicator height just changing its frame, due to framework restrictions. However you should be able to achieve the same result, playing with transform
_indicator.transform = CGAffineTransformMakeScale(1.0f, 0.6f);
EDIT: I just tried on Mac OS X
_indicator.layer.transform = CATransform3DMakeScale(1.0f, 0.6f, 0.0f);
and it doesn't work, so it is not like iOS, likely because of how it is implemented on Cocoa (like Ken suggested).
The only way I managed to change the height is using controlSize, but I don't think it will suit your needs (since it doesn't allow you to specify points).
[_indicator setControlSize:NSMiniControlSize]; // or NSSmallControlSize
You should be able to use an arbitrary frame by subclassing NSProgressIndicator, and overriding drawRect, at this point my recommendation would be to look around to find something that can be extended for your use, like this one
https://www.cocoacontrols.com/controls/lbprogressbar
in IB you can
select your NSProgressIndicator control
in the utilities view select the View Effects inspector
press + in Content Filters
select Lanczos Scale Transform filter
set the appropriate scale value in the Scale row
set the Aspect Ratio too if you need to change the height only
this can be added programmatically also, just google for it how to add Content Filters to NSView
I've a UIButton that has a custom FONT for the titleLabel attribute.
For some reason on iOS 6.0 it shows only half of the title. I tried increasing the height of the title. but that didn't work.
What am i missing?
Is this my only option?
btn.titleLabel.font = [UIFont fontWithName:#"Frutiger95-UltraBlack" size:17];
This is how it should look (minus the color change)
Single line labels have a low content compression resistance priority on the vertical axis. So when you increase the font size, they don't increase the height of their intrinsicContentSize. Setting the compressionResistancePriority to UILayoutPriorityDefaultHigh or UILayoutPriorityRequired should fix it.
[btn.titleLabel setContentCompressionResistancePriority:UILayoutPriorityRequired
forAxis:UILayoutConstraintAxisVertical];
I think updating the label is enough, but you may need to increase the priority on the button itself as well.
[btn setContentCompressionResistancePriority:UILayoutPriorityRequired
forAxis:UILayoutConstraintAxisVertical];
I think you need to set button title nil, then make your own custom UILabel
and add that label on your button like my example given below-
UILabel *lblloginbtntitle=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, btnLogin.frame.size.width, btnLogin.frame.size.height)];
lblloginbtntitle.text=NSLocalizedString(#"Login", nil);
lblloginbtntitle.textAlignment=NSTextAlignmentCenter;
lblloginbtntitle.textColor=[UIColor whiteColor];
lblloginbtntitle.font=[UIFont fontWithName:#"AvenirNextLTPro-Regular" size:20];
[btnLogin addSubview:lblloginbtntitle];
[btnLogin setTitle:#"" forState:UIControlStateNormal]
I create some buttons dynamically on my view and i decide their frames according to some of my JSON respone parameters. But i want to autoresize them when the device(dsimulator) rotates. I can easily do this on interface builder but can't do anything on dynamic ones. Can someone help?
EDIT
Here is a snipped of my code
if (button.tag==1) {
button.frame = CGRectMake(30.0f, yPosition, 200.0f, buttonHeight);
}
if (button.tag==2) {
button.frame = CGRectMake(280.0f, yPosition, 200.0f, buttonHeight);
}
if (button.tag==3) {
button.frame = CGRectMake(530.0f, yPosition, 200.0f, buttonHeight);
}
There is no problem when using Portraid mode but when it rotates to Landscape a big empty area stays on the right side of the screen. How can i fix this?(I mean, when i rotate, i want the buttons got to the center of the scren's width)
EDIT:
I played with autoresizing on Size Inspector(Xcode 4.3) with my xib file and it works great, but whatever i did i couldn't resize the dynamically created buttons after rotation. I tried almost all of AutoresizingMask enums of UIView but nothing changes. Can someone please help
You can define by code what was the expected behavior when the device is rotated.
You can take a look at: http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html
and
http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html#//apple_ref/c/tdef/UIViewAutoresizing
You need to set the button behavior when you add it like:
[button setAutoresizingMask:UIViewAutoresizingFlexibleBottomMargin];
The attributes you've set up in your interface builder file are the UIViewAutoresizing attributes.
Take a look at this documentation from Apple on the UIView class (which your button is a subclass of); look for the UIViewAutoresizing attribute. That's the one you'll want.
Update: Here's a snippet of code for an MKMapView that uses this ability:
mainMapView = [[MKMapView alloc] initWithFrame:CGRectMake(20, 239, 280, 122)];
[mainMapView setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin];
With UIViewAutoresizingFlexibleTopMargin, the map view moves down when the user answers the phone --- it fixes the map's position relative to the top of the screen.
Dig around in the documentation and you'll find the autoresizing mask that works best for your situation.
I'm trying to change the background color of a button when it's selected and don't want to use an image.
[mBtn setBackgroundColor:[UIColor grayColor] forState:UIControlStateHighlighted];
Any thoughts?
I'm replying to this old thread because it pops up consistently in searches for a solution to this problem and I have seen no solution elsewhere. It is truly annoying that setTintColor only applies to the highlighted state of a UIButton. Six months ago, it was equally annoying that it applied only to iOS 5, but that will hopefully be less of an issue going forward. With that in mind, I've drawn upon and combined a number of community suggestions to composite a general purpose solution to tinting a group of buttons in their normal state.
The method below accepts an NSArray of UIButtons and a set of color specifications as input. It applies the color specifications to one button using setTintColor, renders the result to a UIImage, and applies that image as the background image of the entire set of buttons. This avoids the need to create discrete image files for button colors. Also, it does so using a stretchable image so that it may work with a collection of buttons of different sizes (though note that it assumes the default corner rounding factors of UIButton). I hope you'll find it useful for iOS 5 targets.
- (void) setColorOfButtons:(NSArray*)buttons red:(float)red green:(float)green blue:(float)blue alpha:(float)alpha {
if (buttons.count == 0) {
return;
}
// get the first button
NSEnumerator* buttonEnum = [buttons objectEnumerator];
UIButton* button = (UIButton*)[buttonEnum nextObject];
// set the button's highlight color
[button setTintColor:[UIColor colorWithRed:red/255.9999f green:green/255.9999f blue:blue/255.9999f alpha:alpha]];
// clear any existing background image
[button setBackgroundImage:nil forState:UIControlStateNormal];
// place the button into highlighted state with no title
BOOL wasHighlighted = button.highlighted;
NSString* savedTitle = [button titleForState:UIControlStateNormal];
[button setTitle:nil forState:UIControlStateNormal];
[button setHighlighted:YES];
// render the highlighted state of the button into an image
UIGraphicsBeginImageContext(button.layer.frame.size);
CGContextRef graphicsContext = UIGraphicsGetCurrentContext();
[button.layer renderInContext:graphicsContext];
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIImage* stretchableImage = [image stretchableImageWithLeftCapWidth:12 topCapHeight:0];
UIGraphicsEndImageContext();
// restore the button's state and title
[button setHighlighted:wasHighlighted];
[button setTitle:savedTitle forState:UIControlStateNormal];
// set background image of all buttons
do {
[button setBackgroundImage:stretchableImage forState:UIControlStateNormal];
} while (button = (UIButton*)[buttonEnum nextObject]);
}
[mBtn setTintColor:[UIColor grayColor]];
This only effects the highlighted state, so I believe that this is what you're looking for.
You can also set it from Interface Builder, from the Highlight Tint drop-down menu.
Just for people that will land here like I did when searching for changing background colors for highlighted state...
I ended up with an UIButton subclass that has a property for backgroundHighlightColor and tracks highlighting through KVO. Here's the link to GitHub: SOHighlightButton
You should be able to adapt it to any other scenario if you need more / other properties ot the UIButton to change if highlighted.
There is no method like this, setBackgroundColor: forState:
Check documentation. you need to use image.