iOS 7 - Custom UISlider design not working - iphone

Everything works fine in previous version of iOS, but not on iOS 7.
It is not setting custom design for UISlider so there is nothing on the view, the Volume slider view is empty. Everything else works fine.
Code:
for (UISlider *slider in volumeSlider.subviews) {
NSLog(#"Searching!");
if ([slider isKindOfClass:[UISlider class]]) {
NSLog(#"SLIDER FOUND!");
[slider setThumbImage:[UIImage imageNamed:#"thumbSlider.png"] forState:UIControlStateNormal];
[slider setMinimumTrackImage:[[UIImage imageNamed:#"sliderMax.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal ];
[slider setMaximumTrackImage:[[UIImage imageNamed:#"sliderMin.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal ];
}
}

iOS7 is now released so the NDA no longer applies. if you need to support operating systems lower than iOS6 you can use this code. This works on iOS7 as well. Note: if you only need to support iOS6 and later there are direct APIs provided by Apple to set these images to customize the look of the MPVolumeView, specifically setVolumeThumbImage:(UIImage *) forState:(UIControlState), setMinimumVolumeSliderImage:(UIImage *) forState:(UIControlState), and setMaximumVolumeSliderImage:(UIImage *) forState:(UIControlState).
// replace the standard look and feel for the MPVolumeSlider with custom images
// note: on iOS7 it is necessary to set the thumb image last or the thumb will be drawn under the slider (not pretty), on iOS6 the order didn't seem to matter.
UIView *a = nil;
for (UIView *view in [self.volumeView subviews]) {
if ([[[view class] description] isEqualToString:#"MPVolumeSlider"]) {
a=view;
UIImage *volumeBackgroundImage = [[UIImage imageNamed:#"volume_background"] resizableImageWithCapInsets:UIEdgeInsetsMake(9, 5, 7, 5)];
[(UISlider *)a setMinimumTrackImage:[[UIImage imageNamed:[[ApplicationType sharedInstance] imageForAppType:#"volume_progress"]] resizableImageWithCapInsets:UIEdgeInsetsMake(9, 5, 7, 5)] forState:UIControlStateNormal];
[(UISlider *)a setMaximumTrackImage:volumeBackgroundImage forState:UIControlStateNormal];
[(UISlider *)a setThumbImage:[UIImage imageNamed:#"volume_handle"] forState:UIControlStateNormal];
}
}

Possible Apple has changed some API. If you use this code for volume slider of movie player try to use MPVolumeView instead.

Related

UIImage+Retina4 category not working if used in didMoveToSuperview

I'm using this UIImage category to automatically find the correct asset if the app's running on a retina 4 device:
http://www.sourcedrop.net/FY53a14b0127f
It correctly finds the asset with the -568h#2x suffix if the UIImage is instantiated in a UIView subclass's init method:
-(id) init{
self = [super init];
if(self){
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setImage:[UIImage imageNamed:#"go_back_image"] forState:UIControlStateNormal];
[self addSubview:myButton];
}
}
but if the UIImage is instantiated in the class's didMoveToSuperView then the category doesn't pick up the asset:
-(void)didMoveToSuperview{
if(self.superview != nil){
[myButton setImage:[UIImage imageNamed:#"otherImage"] forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:#"otherImageHighlighted"] forState:UIControlStateHighlighted];
}
}
If the UIImage is created in didMoveToSuperview then the normal size asset is shown...
Any thoughts?
Try working with breakpoints in the category code.
I had an issue where an image wasn't picked because I set an image as #"someImage.png" like
[myButton setImage:[UIImage imageNamed:#"someImage.png"] forState:UIControlStateNormal];
instead of simply "someImage" like so
[myButton setImage:[UIImage imageNamed:#"someImage"] forState:UIControlStateNormal];
Found this was the issue by debugging in the category class and used this one line to fix:
//removing png extension, if present
imageNameMutable = (NSMutableString *)[imageNameMutable stringByDeletingPathExtension];
NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageNameMutable ofType:#"png"];
You might have to do something similar. Best of luck :)

Transparent UIToolbar

I wrote the following code to make my toolbar transparent.
[mtoolbar setBackgroundColor:[UIColor clearColor]];
How do I make UIToolbar transparent?
You can set the property translucent to YES and see if this helps.
[self.toolbar setBackgroundImage:[UIImage new]
forToolbarPosition:UIToolbarPositionAny
barMetrics:UIBarMetricsDefault];
[self.toolbar setBackgroundColor:[UIColor clearColor]];
Setting the property translucent to YES will not work in iOS 5 and below. Here's how it can be done without subclassing toolbar:
const float colorMask[6] = {222, 255, 222, 255, 222, 255};
UIImage *img = [[UIImage alloc] init];
UIImage *maskedImage = [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(img.CGImage, colorMask)];
[self.toolbar setBackgroundImage:maskedImage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
Check the below code
[myToolbar setBarStyle:UIBarStyleBlack];
[myToolbar setTranslucent:YES];
Taken from
#Brandon Bodnár has answered in the below SO post.
Couldn't UIToolBar be transparent?
you could also use the different approach
Transparent UIToolBar
for (UIView * sv in [toolBar subviews])
{
[sv removeFromSuperview];
}
;) any iOs
The following works in iOS 5 (and iOS 6 beta 4, although a slight top shadow is still visible there).
Please note:
Making a UIToolbar or UINavigationBar transparent is rarely a good idea, and modifying Apple's UIKit elements in such a way is bound to break sooner or later.
TransparentToolbar.h
#import <UIKit/UIKit.h>
#interface TransparentToolbar : UIToolbar
#end
TransparentToolbar.m
#import "TransparentToolbar.h"
#implementation TransparentToolbar
-(void)insertSubview:(UIView *)view atIndex:(NSInteger)index
{
// This method is called with a view of class "UINavigationBarBackground" or "_UIToolbarBackground", respectively. It would be possible to check for this with NSStringFromClass([view class]) to be completely sure that we're skipping the right view.
if (index != 0)
{
[super insertSubview:view atIndex:index];
}
else
{
// insert your custom background view, if you want to
}
}
#end
EDIT: In iOS 5+, it's also possible to simply set the backgroundImage (which could be transparent). This is certainly the "cleaner" solution, but is less flexible than a custom UIView.
[someToolbar setBackgroundImage:[UIImage imageNamed:#"clear"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
This worked for me for iOS 6 and 7:
UIGraphicsBeginImageContextWithOptions(CGSizeMake(1, 1), NO, 0.0);
UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.toolBar setBackgroundImage:blank forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];

Link to webpage from my app

How do I set up a label in Interface Builder to (when clicked) send the user to a website?
What PengOne said. Or progammatically something like this:
UIButton* button = [UIButton buttonWithType: UIButtonTypeCustom];
[button setTitle:#"obliquely.org.uk" forState:UIControlStateNormal];
[[button titleLabel] setFont: [UIFont systemFontOfSize: 16.0]];
[[button titleLabel] setTextAlignment:UITextAlignmentRight];
[button setTitleColor: [UIColor lightGrayColor] forState:UIControlStateNormal];
[button setTitleColor: [UIColor darkGrayColor] forState:UIControlStateHighlighted];
[button setFrame: CGRectMake (100.0, 100.0, 140.0, 16.0 + 4.0)
[button addTarget:self action:#selector(appWebsite) forControlEvents:UIControlEventTouchUpInside];
This gives a gray link that goes darker when the user taps on it. Doing it programmatically can be helpful if you want to position the link carefully and differently depending on orientation / whether you are on iPad or iPhone / other stuff happening on screen. (Though, of course, you can still use IB and just adjust the frame.)
And then add a method a bit like this:
- (void) appWebsite;
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://obliquely.org.uk/blog/app"]];
}
Add a button,
IBOutlet UIButton *myButton;
and declare an action
-(IBAction)goToWebSite;
Set the button target to the action in the IB. In the .m file, define the action:
-(IBAction)goToWebSite {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.mywebpage.com"]];
}
Use a UITextView to hold the URL text. It will automatically be clickable and will take the user there when clicked. You can give the textView a clear background so it looks just like a label or whatever you want.

How to customize UISwitch button in iphone?

I created a UISwitch using this code...
UISwitch *switch = [[UISwitch alloc]initWithFrame:CGRectMake(110, 230, 60, 60)];
[window addSubview:switchView];
[switchView release];
The created button will be....
The default properties are,
It contains "ON" & "OFF" states
The OFF button is white & the ON button is in blue color
I want to create a customized switch, so that the background color & text in the switch should be changed. Is it possible? Please explain in detail.
Thanks in Advance,
Rajkanth
You can not modify UISwitch control unless and until you write your own control,
But best way so far, you can used UISegmentControl and handle event on it to switch the on.png and off.png images.
UISegmentedControl* switchView=[[UISegmentedControl alloc] initWithItems:[[[NSMutableArray alloc] initWithObjects:#"On",#"Off",nil] autorelease]];
[switchView setFrame:CGRectMake(20,365,140,28)];
switchView.selectedSegmentIndex=0;
switchView.segmentedControlStyle=UISegmentedControlStyleBar;
[switchView setImage:[UIImage imageNamed:#"onSelected.png"] forSegmentAtIndex:0];
[switchView setImage:[UIImage imageNamed:#"off.png"] forSegmentAtIndex:1];
[switchView addTarget:self action:#selector(checkOnOffState:) forControlEvents:UIControlEventValueChanged];
self.navigationItem.titleView=switchView;
and write checkOnOffState method code like this-
-(IBAction)checkOnOffState:(id)sender{
UISegmentedControl* tempSeg=(UISegmentedControl *)sender;
if(tempSeg.selectedSegmentIndex==0){
[tempSeg setImage:[UIImage imageNamed:#"onSelected.png"] forSegmentAtIndex:0];
[tempSeg setImage:[UIImage imageNamed:#"off.png"] forSegmentAtIndex:1];
}
else{
[tempSeg setImage:[UIImage imageNamed:#"on.png"] forSegmentAtIndex:0];
[tempSeg setImage:[UIImage imageNamed:#"offSelected.png"] forSegmentAtIndex:1];
}
}
I used this solution: UICustomSwitch: it works customizing a UISlider and setting a max valure of 1.
You can change the images of your switch, the right / left text and use it with a unique color on background (if you don't want to use images).
The only change I did was about the name: UI is reserved for Apple classe, so I changed it for my own.

What is the best way to make a UIButton checkbox?

I am trying to make a standard check box for my iPhone app from a UIButton with a title and image. The button image changes between an "unchecked" image and a "checked" image.
At first I tried subclassing UIButton but UIButton has no -init*** method to use in my -init method.
What is the best way to do this?
Thanks in advance.
You shouldn't need to subclass the UIButton class. By design, Objective-C favors composition over inheritance.
UIButton is a subclass of UIControl, which has a selected property. You can use this property to toggle the on/off behaviour of a checkbox, just the same way a UISwitch does.
You can attach an action to the button's touched up inside event, and perform the toggling in there, something like this:
// when you setup your button, set an image for the selected and normal states
[myCheckBoxButton setImage:checkedImage forState:UIControlStateSelected];
[myCheckBoxButton setImage:nonCheckedImage forState:UIControlStateNormal];
- (void)myCheckboxToggle:(id)sender
{
myCheckboxButton.selected = !myCheckboxButton.selected; // toggle the selected property, just a simple BOOL
}
Set the images in the button:
[button setImage:uncheckedImage forState:UIControlStateNormal]
[button setImage:checkedImage forState:UIControlStateSelected]
Now all you need to do is:
button.selected = state
and the correct images will display.
All you need to do is set 2 different images for the states UIControlStateNormal and UIControlStateSelected, then in your selector, changed the selected property of the button.
Here is a working example (replace image names with your own):
- (void)loadView {
// ...
UIButton *chkBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[chkBtn setFrame:CGRectMake(0, 0, 300, 25)];
[chkBtn setImage:[UIImage imageNamed:#"UNCHECKED.png"]
forState:UIControlStateNormal];
[chkBtn setImage:[UIImage imageNamed:#"CHECKED.png"]
forState:UIControlStateSelected];
[chkBtn addTarget:self
action:#selector(chkBtnHandler:)
forControlEvents:UIControlEventTouchUpInside];
// Optional title change for checked/unchecked
[chkBtn setTitle:#"I am NOT checked!"
forState:UIControlStateNormal];
[chkBtn setTitle:#"I am checked!"
forState:UIControlStateSelected];
[self.view addSubview:chkBtn];
[chkBtn release], chkBtn = nil;
// ...
}
- (void)chkBtnHandler:(UIButton *)sender {
// If checked, uncheck and visa versa
[sender setSelected:!sender isSelected];
}
For anyone interested in the future - instead of doing it yourself just download the link below from GitHub and it has it subclassed from UIControl already and functions perfectly as a checkbox. Also includes a sample project on how easy it is to use:
https://github.com/Brayden/UICheckbox
I have used M13Checkbox in one of my projects. Works ok.
https://github.com/Marxon13/M13Checkbox
Did you try overriding the initWithCoder method, just in case it is loaded from a nib somehow?
UIImage* nonCheckedImage=[UIImage imageNamed:#"ic_check_box_outline_blank_grey600_48dp.png"];//[UIImage init
UIImage* CheckedImage=[UIImage imageNamed:#"ic_check_box_black_48dp.png"];//[UIImage init
//ic_check_box_black_48dp.png
[_checkBox setImage:CheckedImage forState:UIControlStateSelected];
[_checkBox setImage:nonCheckedImage forState:UIControlStateNormal];
}
- (IBAction)checkBoxToggle:(id)sender {
_checkBox.selected = !_checkBox.selected; // toggle the selected property, just a simple BOOL
}
the image you can use google icon
Try this:-
-(IBAction)clickCheckButton:(UIButton *)sender {
if (sender.tag==0) {
sender.tag = 1;
[sender setImage:[UIImage imageNamed:#"check.png"] forState:UIControlStateNormal];
}else
{
sender.tag = 0;
[sender setImage:[UIImage imageNamed:#"uncheck.png"] forState:UIControlStateNormal]; } } sender.tag = 0;
[sender setImage:[UIImage imageNamed:#"uncheck.png"] forState:UIControlStateNormal];
}
}
Why not use a switch - UISwitch? This is used to display an element showing the boolean state of a value.