UISegmentedControl setImage: Bug in iOS7 - objective-c

I have a UISegmentedControl in my app. As of iOS7 GM, the images I use are not showing up when run on iOS7 devices. Anyone else having this problem?
Here's what it looks like in iOS6.1 and earlier
.
and here is what it looks like in iOS7
.
Here is the code:
self.theSegmentedControl.frame = CGRectMake(self.theSegmentedControl.frame.origin.x, self.theSegmentedControl.frame.origin.y, 320, 35);
[self.theSegmentedControl setBackgroundImage:[UIImage imageNamed:#"img_toggleInactive"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.theSegmentedControl setImage:[UIImage imageNamed:#"btn_onceActive"] forSegmentAtIndex:0];
[self.theSegmentedControl setImage:[UIImage imageNamed:#"btn_recurringInactive"] forSegmentAtIndex:1];
[self.theSegmentedControl setImage:[UIImage imageNamed:#"btn_scheduledInactive"] forSegmentAtIndex:2];
[self.theSegmentedControl setDividerImage:[UIImage imageNamed:#"separator"] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
Has anyone found a workaround to this?

Woohoo! Here's the workaround:
//Add clear color to mask any bits of a selection state that the object might show around the images
self.theSegmentedControl.tintColor = [UIColor clearColor];
UIImage *onceActive;
UIImage *recurringActive;
UIImage *scheduledActive;
UIImage *separator;
//Setting imageWithRenderingMode: to imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal for iOS7 is key
if ([UIImage instancesRespondToSelector:#selector(imageWithRenderingMode:)]) {
onceActive = [[UIImage imageNamed:#"btn_onceActive"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
recurringActive = [[UIImage imageNamed:#"btn_recurringInactive"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
scheduledActive = [[UIImage imageNamed:#"btn_scheduledInactive"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
separator = [[UIImage imageNamed:#"separator"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
else {
onceActive = [UIImage imageNamed:#"btn_onceActive"];
recurringActive = [UIImage imageNamed:#"btn_recurringInactive"];
scheduledActive = [UIImage imageNamed:#"btn_scheduledInactive"];
separator = [UIImage imageNamed:#"separator"];
}
[self.theSegmentedControl setImage:onceActive forSegmentAtIndex:0];
[self.theSegmentedControl setImage:recurringActive forSegmentAtIndex:1];
[self.theSegmentedControl setImage:scheduledActive forSegmentAtIndex:2];
[self.theSegmentedControl setDividerImage:separator forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

UPDATE for Xcode 6/iOS 8
Now you can do it in Interface builder
Just add the image file in the asset catalog and set its "render as" original image instead of default
Xcode 5
The new UISegmented control uses the tint color to tint the images using the template mode.
You will need to render these images as original and not templates.
As suggested in the comments do this:
UIImage* onceActive = [UIImage imageNamed:#"btn_onceActive"];
if (IOS_7_MACRO)
onceActive = [onceActive imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[self.theSegmentedControl setImage:onceActive forSegmentAtIndex:0];

Use this Code to set Image on Segment Control in iOS 7 with xCode 5.0
if ([UIImage instancesRespondToSelector:#selector(imageWithRenderingMode:)]) {
[segmentControl setImage:[[UIImage imageNamed:#"image.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forSegmentAtIndex:0];
}
else {
[segmentControl setImage:[UIImage imageNamed:#"image.png"] forSegmentAtIndex:0];
}

It could be useful to create a category:
#interface UISegmentedControl (UISegmentedControlAdditions)
-(void)setImageRenderingMode:(UIImageRenderingMode)renderingMode;
#end
#implementation UISegmentedControl (UISegmentedControlAdditions)
-(void)setImageRenderingMode:(UIImageRenderingMode)renderingMode {
for (int index=0; index < [self numberOfSegments]; index++) {
UIImage * image = [self imageForSegmentAtIndex:index];
[self setImage:[image imageWithRenderingMode:renderingMode] forSegmentAtIndex:index];
}
}
... and just call
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
[colorSegmentedControl setImageRenderingMode:UIImageRenderingModeAlwaysOriginal];

Related

UIButton tap area got smaller on iOS 11

I notice the tap area for my UIButton got smaller since iOS 11. To confirm, I commented out the desired background color on its subviews and put purple color on the button itself.
iOS 10.0.3 (Button shown as desired)
iOS 11.1 (Tap area got smaller)
My code follows.
UIImage *ringImage = [UIImage imageNamed:#"ball20r"];
UIEdgeInsets insets = UIEdgeInsetsMake(10,10,10,10); //padding
UIImage *stretchableImage = [ringImage resizableImageWithCapInsets:insets];
UIImageView *imageView = [[UIImageView alloc]initWithImage:stretchableImage];
// imageView.backgroundColor = backgroundColor; //disabled for testing
imageView.frame = CGRectMake(0, 0, label.frame.size.width + 16.0, label.frame.size.height + 16.0);
[imageView addSubview:label];
label.translatesAutoresizingMaskIntoConstraints = NO;
NSDictionary *viewsDictionary = #{#"label":label};
NSArray *constraint_H = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|-(8)-[label]-(8)-|"
options:0
metrics:nil
views:viewsDictionary];
NSArray *constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:#"H:|-(8)-[label]-(8)-|"
options:0
metrics:nil
views:viewsDictionary];
[imageView addConstraints:constraint_H];
[imageView addConstraints:constraint_V];
UIButton *calendarButton = [UIButton buttonWithType:UIButtonTypeCustom];
[calendarButton addTarget:self action:#selector(chooseACalendarToSave) forControlEvents:UIControlEventTouchUpInside];
calendarButton.frame = imageView.frame;
[calendarButton addSubview:imageView];
//added for testing only
calendarButton.backgroundColor = [UIColor purpleColor];
_calendarButton = [[UIBarButtonItem alloc]initWithCustomView:calendarButton];
How can I make the area for iOS just like iOS 10.0.3 and earlier?
Thanks for your help.
The answer is here.
iOS 11 UIBarButtonItem images not sizing
if (#available(iOS 9, *)) {
[cButton.widthAnchor constraintEqualToConstant: standardButtonSize.width].active = YES;
[cButton.heightAnchor constraintEqualToConstant: standardButtonSize.height].active = YES;
}
As soon as I inserted above code, the button started working as expected. Thank you.

How to use animated Navigation bar button in Objective c

I want to add this Image on my Navigation Bar button, How can I use this image on my navigation Right Bar button or Left bar button?
Please check below code those I am using to View same like work code on Navigation bar button:-
- (void)viewDidLoad
{
[super viewDidLoad];
// Animation
UIImageView*animationView = [[UIImageView alloc] initWithFrame:self.view.frame];
animationView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"1.gif"],
[UIImage imageNamed:#"2.gif"],
[UIImage imageNamed:#"3.gif"],
[UIImage imageNamed:#"4.gif"],
[UIImage imageNamed:#"5.gif"],
[UIImage imageNamed:#"6.gif"],nil];
animationView.animationDuration = 1.25;
animationView.animationRepeatCount = 0;
[animationView startAnimating];
[self.view addSubview:animationView];
}
Let me know How
to use this code on navigation bar button.
Thank You!
I have tried your code, with a little trick of mine and it work like magic.
NSArray *imageArray = [NSArray arrayWithObjects:
[UIImage imageNamed:#"tmp-0"],
[UIImage imageNamed:#"tmp-1"],
[UIImage imageNamed:#"tmp-2"],
[UIImage imageNamed:#"tmp-3"],
[UIImage imageNamed:#"tmp-4"],
[UIImage imageNamed:#"tmp-5"],
[UIImage imageNamed:#"tmp-6"],
[UIImage imageNamed:#"tmp-7"],nil];
UIButton *barButton = [UIButton buttonWithType:UIButtonTypeCustom];
[barButton setImage:[UIImage imageNamed:#"tmp-0"] forState:UIControlStateNormal]; // mine trick
[barButton.imageView setAnimationImages:imageArray];
[barButton.imageView setAnimationDuration:1.0f];
[barButton.imageView startAnimating];
[barButton sizeToFit];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:barButton];
You need to give a image to barButton at first, so your button can make its frame

How can I Use Animation On Navigation Right bar button?

I am using below code for Navigation right bar button:--
UIButton *settingsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[settingsBtn setImage:[UIImage imageNamed:#"logo_soft.png"] forState:UIControlStateNormal];
//[settingsBtn addTarget:self action:#selector(settings) forControlEvents:UIControlEventTouchUpInside];
[settingsBtn setFrame:CGRectMake(-16, -6, 95, 43)];
UIView *rightBarButtonItems = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 76, 32)];
//[rightBarButtonItems addSubview:homeBtn];
[rightBarButtonItems addSubview:settingsBtn];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBarButtonItems];
Now I want to use below code on same Navigation right bar button:
- (void)viewDidLoad
{
[super viewDidLoad];
// Animation
UIImageView*animationView = [[UIImageView alloc] initWithFrame:self.view.frame];
animationView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"1.gif"],
[UIImage imageNamed:#"2.gif"],
[UIImage imageNamed:#"3.gif"],
[UIImage imageNamed:#"4.gif"],
[UIImage imageNamed:#"5.gif"],
[UIImage imageNamed:#"6.gif"],nil];
animationView.animationDuration = 1.25;
animationView.animationRepeatCount = 0;
[animationView startAnimating];
[self.view addSubview:animationView];
}
How to work my navigation bar button like the below Images:---
Please send me suitable answer or provide me sample code.
Thank you.
I have tried your code, with a little trick of mine and it work like magic.
NSArray *imageArray = [NSArray arrayWithObjects:
[UIImage imageNamed:#"tmp-0"],
[UIImage imageNamed:#"tmp-1"],
[UIImage imageNamed:#"tmp-2"],
[UIImage imageNamed:#"tmp-3"],
[UIImage imageNamed:#"tmp-4"],
[UIImage imageNamed:#"tmp-5"],
[UIImage imageNamed:#"tmp-6"],
[UIImage imageNamed:#"tmp-7"],nil];
UIButton *barButton = [UIButton buttonWithType:UIButtonTypeCustom];
[barButton setImage:[UIImage imageNamed:#"tmp-0"] forState:UIControlStateNormal]; // mine trick
[barButton.imageView setAnimationImages:imageArray];
[barButton.imageView setAnimationDuration:1.0f];
[barButton.imageView startAnimating];
[barButton sizeToFit];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:barButton];
You need to give a image to barButton at first, so your button can make its frame

The title of SegmentControl is distorted after Customising using UIAppearance

After customizing, the text position was distorted..
code used to customize:
+ (void)customiseSegmentControl {
UIImage *selectedImage = [[UIImage imageNamed:#"SegmentSelected.png"]
stretchableImageWithLeftCapWidth:5 topCapHeight:1];
UIImage *unselectedImage = [[UIImage imageNamed:#"SegmentUnselected.png"]
stretchableImageWithLeftCapWidth:5 topCapHeight:1];
[[UISegmentedControl appearance] setBackgroundImage:selectedImage forState:(UIControlStateSelected|UIControlStateHighlighted) barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setBackgroundImage:unselectedImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
}
http://idevrecipes.com/2010/12/11/custom-segmented-controls/
Check this link.. and code
NSArray *segmentTextContent = [NSArray arrayWithObjects:#"First",#"Second",#"Third",#"Forth", nil];
segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentTextContent];
segmentedControl.frame = CGRectMake(2, 5, 316, 35);
[segmentedControl addTarget:self action:#selector(segmentAction)forControlEvents:UIControlEventValueChanged];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.enabled = true;
segmentedControl.selectedSegmentIndex = 0;
// cutomize the font size inside segmentedControl
for (id segment in [segmentedControl subviews])
{
for (id label in [segment subviews])
{
if ([label isKindOfClass:[UILabel class]])
{
[label setTextAlignment:UITextAlignmentCenter];
[label setFont:[UIFont boldSystemFontOfSize:11]];
//[label setTextColor:[UIColor greenColor]];
}
}
}

UINavigation bar button background image size

I'm trying sot set image background for the left back button navigation bar for all the app with this snippet:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version >= 5.0)
{
// iPhone 5.0 code here
UIImage *image = [UIImage imageNamed: #"btn_back.png"];
image = [image stretchableImageWithLeftCapWidth:40.0f topCapHeight:0.0f];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:image forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
}
return YES;
}
I've been trying different values in stretchableImageWithLeftCapWidth: but best result was this:
How can I set background image to the correct size?
Thanks
stretchableImageWithLeftCapWidth:topCapHeight: is deprecated. Use resizableImageWithCapInsets: instead.
This should work:
UIImage *buttonImage = [[UIImage imageNamed:#"btn_back.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(1, 10, 1, 10)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];