How do I visually indicate that a UIAlertView is Important? - ios

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.

Related

Cannot set UISearchBar's translucent property to NO

I am trying to set the barTintColor of a UISearchBar without translucency. However, setting the translucent property doesn't seem to do anything. I have reproduced the issue in a bare-bones Xcode project here.
self.searchDisplayController.searchBar.translucent = NO;
self.searchDisplayController.searchBar.barTintColor = [UIColor redColor];
The red color above is not the same as [UIColor redColor] in UIViews that are not translucent. I know about the workaround involving setting a background image on the search bar, but the above code should work as well.
I downloaded your code and find solution for, add one method name is removeUISearchBarBackgroundInViewHierarchy and set searchBar.backgroundColor as redColor.
- (void)viewDidLoad
{
[super viewDidLoad];
self.searchDisplayController.searchBar.translucent = NO;
[self removeUISearchBarBackgroundInViewHierarchy:self.searchDisplayController.searchBar];
self.searchDisplayController.searchBar.backgroundColor = [UIColor redColor];
}
- (void) removeUISearchBarBackgroundInViewHierarchy:(UIView *)view
{
for (UIView *subview in [view subviews]) {
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")]) {
[subview removeFromSuperview];
break; //To avoid an extra loop as there is only one UISearchBarBackground
} else {
[self removeUISearchBarBackgroundInViewHierarchy:subview];
}
}
}
Please set image in search bar like this it will solve your problem.
[[UISearchBar appearance] setBackgroundImage:[UIImage imageNamed:#"red"]];
Thanks.
Maybe too late for the party,
However I've done a very simple and nifty extension for Swift 3 that allows you to use the translucent property without any trouble.
It involves no use of private API or dangerous walk-through within the object.
You can download it from this Github repository.

UIButton subclass highlighting error (highlight remains or persists even after tap or touch down)

I subclassed UIButton in my app and there are many times when the highlight color stays even when I'm done pressing down the button. I can't figure out exactly what causes this since it only seems to happen by chance, but it seems to happen about 50% of the time. I'm very sure that this is reproducible. I often get this to happen when I have a button in a UITableViewCell and I click on it while the table view is still scrolling.
Is there something wrong with the way I'm overriding the setHighlighted method in the subclass? This is my implementation:
#implementation SCPFormButton
- (id)initWithFrame:(CGRect)frame label:(NSString *)label
{
self = [super initWithFrame:frame];
if (self) {
UILabel *buttonLabel = [[UILabel alloc] init];
buttonLabel.attributedText = [[NSAttributedString alloc] initWithString:[label uppercaseString] attributes:kButtonLabelAttributes];
[buttonLabel sizeToFit];
buttonLabel.frame = CGRectMake(kMaxWidth / 2 - buttonLabel.frame.size.width / 2, kStandardComponentHeight / 2 - buttonLabel.frame.size.height / 2, buttonLabel.frame.size.width, buttonLabel.frame.size.height);
[self addSubview:buttonLabel];
self.backgroundColor = kFormButtonColorDefault;
}
return self;
}
- (void)setHighlighted:(BOOL)highlighted
{
self.backgroundColor = highlighted ? kFormButtonColorHighlighted : kFormButtonColorDefault;
[self setNeedsDisplay];
}
#end
I would try to call super in your setHighlighted override. Indeed, Apple docs for UIControl state:
Specify YES if the control is highlighted; otherwise NO. By default, a control is not highlighted. UIControl automatically sets and clears this state automatically when a touch enters and exits during tracking and when there is a touch up.
So, it seems there is some kind of state handling going on in UIControl associated with this.
If this does not help, I would try to add a log trace so you can check which state the button is in when the touch is handled.
You are missing the call to super. But, anyway, subclassing UIButton is not recommended, I would try to do it using setBackgroundImage:forState instead.

How to right align text of UISearchbar in iOS7

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;
}

Set Color of UIActivityIndicatorView of a UIRefreshControl?

Is there a way to set the color of the activity indicator (probably UIActivityIndicatorView) of a UIRefreshControl?
I was able to set the color of the 'rubber' and the indicator:
[_refreshControl setTintColor:[UIColor colorWithRed:0.0f/255.0f green:55.0f/255.0f blue:152.0f/255.0f alpha:1.0]];
But I want to have the 'rubber' blue and the activity indicator white, is this possible?
This is not officially supported, but if you want to risk future iOS changes breaking your code you can try this:
Building off ayoy's answer, I built a subclass of UIRefreshControl, which sets the color of the ActivityIndicator in beginRefresing. This should be a better place to put this, since you may call this in code instead of a user causing the animation to begin.
#implementation WhiteRefreshControl : UIRefreshControl
- (void)beginRefreshing
{
[super beginRefreshing];
NSArray *subviews = [[[self subviews] lastObject] subviews];
//Range check on subviews
if (subviews.count > 1)
{
id spinner = [subviews objectAtIndex:1];
//Class check on activity indicator
if ([spinner isKindOfClass:[UIActivityIndicatorView class]])
{
UIActivityIndicatorView *spinnerActivity = (UIActivityIndicatorView*)spinner;
spinnerActivity.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
}
}
}
Well, you technically can do it, but it's not supported. You better follow Dave's suggestion, or read on if you insist.
If you investigate subviews of UIRefreshControl it turns out that it contains one subview, of class _UIRefreshControlDefaultContentView.
If you then check subviews of that content view in refreshing state, it contains the following:
UILabel
UIActivityIndicatorView
UIImageView
UIImageView
So technically in your callback to UIControlEventValueChanged event you can do something like this:
UIActivityIndicatorView *spinner = [[[[self.refreshControl subviews] lastObject] subviews] objectAtIndex:1];
spinner.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
And that would work. It also doesn't violate App Review Guidelines as it doesn't use private API (browsing subviews of a view and playing with them using public API is legal).
But keep in mind that the internal implementation of UIRefreshControl can change anytime and your code may not work or even crash in later versions of iOS.
No, that's not possible. You'll need to file an enhancement request to ask Apple to implement this.

how to create UISegmentedControl to look like buttons on camera

I'd like to create a UISegmentedControl that's styled similarly to the flash and hdr controls in the camera app. (i.e. black outline, black text, frosted semi-translucent background)
Any suggestions to do this?
Thanks,
You might want to follow this tutorial http://www.raywenderlich.com/4344/user-interface-customization-in-ios-5 to understand how to customize UIKit controls with your own assets.
The idea is that you will use UIAppearance and will have to create a few assets for the various possible states (selected/unselected, left/right...)
#JP... Thanks for the answer. In the interim, I found a way to do it.
I'm placing the segmented controller as an overlay on a map to change the map's style (standard/satellite/hybrid) as in google maps. Note the subtle change in the background color - that's to increase readability on standard-style maps which tend do have a much lighter background than satellite-based images.
- (void) setMapSelectorColors:(UISegmentedControl *)control {
NSDictionary *mapStyleSelectorTextAttributes = [NSDictionary dictionaryWithObject:[UIColor blackColor] forKey:UITextAttributeTextColor];
[self.mapStyleSelector setTitleTextAttributes:mapStyleSelectorTextAttributes forState:UIControlStateNormal];
if (control.selectedSegmentIndex == 0) {
self.mapStyleSelector.tintColor = [UIColor lightGrayColor];
} else {
self.mapStyleSelector.tintColor = [UIColor whiteColor];
}
}

Resources