I'm using UITextField as a UISearchBar replacement and "stealing" the magnifying glass icon from the original UISearchBar with this crazy code:
UISearchBar *originalSearchBar = [[UISearchBar alloc] init];
for (UIView *searchBarSubview in [originalSearchBar subviews]) {
if([searchBarSubview isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField *)searchBarSubview;
[_textField setLeftView:[textField leftView]];
[_textField setLeftViewMode:UITextFieldViewModeAlways];
}
}
As you've probably guessed, I don't want to use my own bitmap.
Isn't there an easier accessible magnifying glass icon somewhere in Cocoa?
So, here's the code with the unicode character:
UILabel *magnifyingGlass = [[UILabel alloc] init];
[magnifyingGlass setText:[[NSString alloc] initWithUTF8String:"\xF0\x9F\x94\x8D"]];
[magnifyingGlass sizeToFit];
[textField setLeftView:magnifyingGlass];
[textField setLeftViewMode:UITextFieldViewModeAlways];
Edit: For plain look that fits iOS 7 style, add Unicode variation selector \U000025B6.
I don't know of any standard image for it in Cocoa (and there is no character for it in Unicode either).
An image for this is part of the Dock bundle, however:
/System/Library/CoreServices/Dock.app/Contents/Resources/ectl_search_magnify.png
As I couldn't get a plain unicode magnifying glass to appear, I decided to see what a standard UISearchBox uses. It turns out it's an image of a magnifying glass, which I've extracted and included below, though it's a very simple shape which would be trivial to reproduce perfectly.
Using OSX 10.7.5 I couldn't find the ectl_search_magnify.png but a simple
find /System -ipath '*magni*'
found:
/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/MagnifyingGlassIcon.icns
/System/Library/PrivateFrameworks/ProKit.framework/Versions/A/Resources/LionPanels.bundle/Contents/Resources/Dark_NSSmallMagnifyingGlass.tiff
/System/Library/PrivateFrameworks/ProKit.framework/Versions/A/Resources/LionPanels.bundle/Contents/Resources/Night_NSSmallMagnifyingGlass.tiff
The latter two are probably what you want (assuming you need an image).
Related
Could you tell me how to right align UISearchbar text in iOS 7? , I was using this in iOS6 but now it does not work in iOS7:
//hacking search bar
UITextField *searchField;
for (UIView *subview in self.searchBar.subviews)
{
if ([subview isKindOfClass:[UITextField class]]) {
searchField = (UITextField *)subview;
break;
}
}
if (searchField) {
searchField.textAlignment = NSTextAlignmentRight;
}
Unfortunately this cannot be done safely without completely re-implementing the class from scratch, as text alignment is adjusted by the internals of the object's code when the user begins and finishes editing.
The closest thing to what you want to do would be to use the three position adjustments to shift the text horizontally, but this doesn't affect alignment, only absolute position, and even then only when the user is typing.
If you want to try this, look up searchTestPositionAdjustment, setPositionAdjustment:forSearchBarIcon:, and searchFieldBackgroundPositionAdjustment. I don't think it will be of much use to you though.
-Ash
It's too late, but if anyone is still wondering the solution, then you can follow this.
UITextField *searchTextField = [searchBarController.searchBar valueForKey:#"_searchField"];
You can get the search field using above code. Now simply use the properties you want to use, like.
searchTextField.layer.cornerRadius = 10.0f;
searchTextField.textAlignment = NSTextAlignmentRight;
I've got a solution to this problem. It's a bit hacky and not very neat, but it does the trick.
Since UISearchBar itself does not allow you to edit the placeholder, I've set a UITextField underneath it and disabled it from any touches by doing this:
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if([touch.view isDescendantOfView:self.placeHolderTextField]){
return NO;
}
return YES;
}
Note: don't forget to include <UIGestureRecognizerDelegate> in your .h file.
If this doesn't work however, you can always use [self.view sendSubViewToBack:self.placeholderTextField];
Next on, I've just set events on which I want to display the placeholder and when not.
In the viewDidLoad, I'm just calling self.placeHolderTextFiew.placeholder = #"search"
And in
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if(searchText.length > 0){
self.placeHolderTextfield.placeholder = #"";
} else{
self.placeHolderTextfield.placeholder = #"search";
}
}
Note again: Make sure to include <UISearchBarDelegate> in your .h file in order for this to work.
I am using a tableView as well, so when the method DidSelectRowAtIndexPath is called, I'm also setting the placeholder to an empty string.
Hope this helps.
you can try like this also..
searchbar.placeholder = #"Hai.. whitespace ";
way to set text right align
searchbar->attribute inspector->search text->custom offset->horizontal(set as per requirement)
After playing with subviews of UISearchBar I found this solution, it works for iOS 6 and iOS 7
//hacking search bar
UITextField *searchField;
for (UIView *subview in self.searchBar.subviews)
{
//this will work in iOS 7
for (id sub in subview.subviews) {
if([NSStringFromClass([sub class]) isEqualToString:#"UISearchBarTextField"])
{
[sub setTextAlignment:NSTextAlignmentRight];
}
}
//this will work for less than iOS 7
if ([subview isKindOfClass:[UITextField class]]) {
searchField = (UITextField *)subview;
break;
}
}
//for less than iOS 7
if (searchField) {
searchField.textAlignment = NSTextAlignmentRight;
}
I'm using UITextField as a UISearchBar replacement and "stealing" the magnifying glass icon from the original UISearchBar with this crazy code:
UISearchBar *originalSearchBar = [[UISearchBar alloc] init];
for (UIView *searchBarSubview in [originalSearchBar subviews]) {
if([searchBarSubview isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField *)searchBarSubview;
[_textField setLeftView:[textField leftView]];
[_textField setLeftViewMode:UITextFieldViewModeAlways];
}
}
As you've probably guessed, I don't want to use my own bitmap.
Isn't there an easier accessible magnifying glass icon somewhere in Cocoa?
So, here's the code with the unicode character:
UILabel *magnifyingGlass = [[UILabel alloc] init];
[magnifyingGlass setText:[[NSString alloc] initWithUTF8String:"\xF0\x9F\x94\x8D"]];
[magnifyingGlass sizeToFit];
[textField setLeftView:magnifyingGlass];
[textField setLeftViewMode:UITextFieldViewModeAlways];
Edit: For plain look that fits iOS 7 style, add Unicode variation selector \U000025B6.
I don't know of any standard image for it in Cocoa (and there is no character for it in Unicode either).
An image for this is part of the Dock bundle, however:
/System/Library/CoreServices/Dock.app/Contents/Resources/ectl_search_magnify.png
As I couldn't get a plain unicode magnifying glass to appear, I decided to see what a standard UISearchBox uses. It turns out it's an image of a magnifying glass, which I've extracted and included below, though it's a very simple shape which would be trivial to reproduce perfectly.
Using OSX 10.7.5 I couldn't find the ectl_search_magnify.png but a simple
find /System -ipath '*magni*'
found:
/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/MagnifyingGlassIcon.icns
/System/Library/PrivateFrameworks/ProKit.framework/Versions/A/Resources/LionPanels.bundle/Contents/Resources/Dark_NSSmallMagnifyingGlass.tiff
/System/Library/PrivateFrameworks/ProKit.framework/Versions/A/Resources/LionPanels.bundle/Contents/Resources/Night_NSSmallMagnifyingGlass.tiff
The latter two are probably what you want (assuming you need an image).
I'm building an application that needs an MPVolumeView to control the volume. It worked perfectly before iOS 5.1 but since the 5.1 update the thumb image is no longer vertically centered. I tried a few things like changing imagine dimensions, resizing my views (and slider) but nothing seems to work, the thumb is just not vertically centered anymore. The only way i get a centered thumb is if i use the default iOS one.
I tried adding a UISlider to another view with the exact min, max and thumb image and that one is centered fine.
Here is the code for the MPVolumeView:
MPVolumeView *volumeView;
volumeView = [[[MPVolumeView alloc] initWithFrame:volumeViewHolder.bounds] autorelease];
[volumeViewHolder addSubview:volumeView];
UIView *volumeViewSlider;
for (UIView *view in [volumeView subviews])
{
if ([[[view class] description] isEqualToString:#"MPVolumeSlider"])
{
volumeViewSlider = view;
}
}
[(UISlider *)volumeViewSlider setThumbImage:sliderHandleIcon forState:UIControlStateNormal];
[(UISlider *)volumeViewSlider setMinimumTrackImage:leftTrackImage forState:UIControlStateNormal];
[(UISlider *)volumeViewSlider setMaximumTrackImage:rightTrackImage forState:UIControlStateNormal];
volumeViewHolder is just a UIView thats 153x33. I put the thumb in green in the screenshot.
Maybe a better solution:
User a bigger image with a transparent border on the bottom. Should be around 10px for Retina Displays.
the same problem i resolved in one project. Must be set color of left part and right part with alpha = 0 -it means transparent all slider without thumb (without moovable part of it). After we must create custom view for line of slider, without thumb. In this view any colored part may be shifted as you want, upper or below, left or right. It obtained using the defined y for your ocassion:
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(x,y,width, height)];
And add the slider to this line as subview. Resulted view will be slider. For example:
UISlider *ourSlider = ...;
//initialise UISlider
ourSlider.minimumTrackTintColor = [UIColor colorWithRed:0 green:122.0f/255.0f blue:1 alpha:0];
ourSlider.minimumTrackTintColor = [UIColor colorWithRed:0 green:122.0f/255.0f blue:1 alpha:0];
UIView *lineOfSliderWithoutThumb = ... ;
// creation it
[lineOfSliderWithoutThumb addSubview:ourSlider];
//after this lineOfSliderWithoutThumb is the our custom uislider.
Note: colors there are used as default slider colors of left and right sides of UISlider.
I am using monotouch but will accept Objective-C answers.
I'd like a visual indication of whether or not a UIAlertView message is important or not.
I found an Objective-C example of how to show an icon in the alert (somewhere here on stackoverflow) but the text does not wrap around the icon.
Perhaps there is another alternative. Maybe there is a way to change the background color of the alert to yellow?
So my specific question is whether or not there is a standard practice for this or can someone recommend a good solution?
Thanks
Apple's HIG emphatically suggests that the appearance of an AlertView should be coordinated with the color scheme of the background. The only visual indication that they suggest is the use of the red button for potentially destructive actions. I agree with #MDT that all alerts should be, by definition, important, and non-important messages (e.g. routine status messages), should be presented in some other manner.
In General people prefer not to customize it much though. Here's some UI Guideline by Apple.
Yes you can customize UIAlertView but for that you will have to subClass it and override it's methods :-
#interface CustomAlertView : UIAlertView
#end
in .m file override the method layoutSubviews:-
- (void)layoutSubviews
{
for (UIView *subview in self.subviews){ //Fast Enumeration
if ([subview isMemberOfClass:[UIImageView class]]) {
subview.hidden = YES; //Hide UIImageView Containing Blue Background
}
if ([subview isMemberOfClass:[UILabel class]]) { //Point to UILabels To Change Text
UILabel *label = (UILabel*)subview; //Cast From UIView to UILabel
label.textColor = [UIColor colorWithRed:210.0f/255.0f green:210.0f/255.0f blue:210.0f/255.0f alpha:1.0f];
label.shadowColor = [UIColor blackColor];
label.shadowOffset = CGSizeMake(0.0f, 1.0f);
}
}
}
and then you will have to override drawRect , You can see this tutorial on CustomAlert if you want totally different Alert.
I have a UIPickerView which works correctly, now I want to add a button above it so that I can dismiss it.
and here is my code where I initiate a UIPickerView as well as its dismiss button:
- (UIPickerView *)creatPickerView {
UIPickerView *tempPickerView = [[[UIPickerView alloc]
initWithFrame:CGRectMake(kPickerViewX, kPickerViewY, kPickerViewWidth, kPickerViewHeight)] autorelease];
tempPickerView.showsSelectionIndicator = YES;
tempPickerView.delegate = self;
tempPickerView.dataSource = self;
UIButton *pickerButton = [[UIButton alloc] initWithFrame:CGRectMake(270, -32, 50, 32)];
[pickerButton setBackgroundImage:[UIImage imageNamed:#"hidePicker.png"]
forState:UIControlStateNormal];
[pickerButton addTarget:self action:#selector(hidePicker)
forControlEvents:UIControlEventTouchUpInside];
[tempPickerView addSubview:pickerButton];
[pickerButton release];
[self.view addSubview:tempPickerView];
return tempPickerView;
}
and it works well on my iPhone 4.3 Simulator, like this:
apparently there is a button on the upper right of the pickerView,
problem is, when I run the app in my device - a 5.0.1 iPhone4 and a 4.2.1 iTouch, the button is missed like it has never been added to the pickerView.
Can anyone help me with this?
Thanks a lot and a lot!
I found the reason, it seems the png has some problem,
after I change another png, it comes up in the screen!
but the real problem is that I place the button outside of the pickerView which results in the button's untouchableness.
But anyway the pickure is only a small problem.