How to set UIActionSheet button color - ios

I've been searching the internet and trying different thing but cannot seem to change the button title colour on my action sheet.
Here is the action sheet code.
- (void)showActionSheet
{
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:#"Select Country"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
for (NSString *listCountry in resultSet) [actionSheet addButtonWithTitle:listCountry];
[actionSheet showInView:[self.view window]];
}
I've tried this delegate but it does not work.
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
for (UIView *subview in actionSheet.subviews) {
if ([subview isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)subview;
button.titleLabel.textColor = [UIColor greenColor];
}
}
}

Make sure you set, UIActionSheet delegate and set breakpoint on this method,
willPresentActionSheet,
check whether this method getting called .
If you have set delegate means, try this one
- (void) changeTextColorForUIActionSheet:(UIActionSheet*)actionSheet {
UIColor *tintColor = [UIColor redColor];
NSArray *actionSheetButtons = actionSheet.subviews;
for (int i = 0; [actionSheetButtons count] > i; i++) {
UIView *view = (UIView*)[actionSheetButtons objectAtIndex:i];
if([view isKindOfClass:[UIButton class]]){
UIButton *btn = (UIButton*)view;
[btn setTitleColor:tintColor forState:UIControlStateNormal];
}
}
}
Update:
https://github.com/seivan/SHActionSheetBlocks
https://github.com/ianb821/IBActionSheet

You must implement UIActionSheetDelegate and then you can use willPresentActionSheet delegate method
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
for (UIView *subview in actionSheet.subviews) {
if ([subview isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)subview;
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
}
}

Related

Objective-c App crashing in iOS 13(Search bar issue)

Please someone help me I am not familiar with Objective-c. App is crashing in iOS 13
found the following error in console
* Terminating app due to uncaught exception 'NSGenericException', reason: 'Missing or detached view for search bar layout. The application must not remove > from the hierarchy.'
Search bar code
UIImageView *searchBackView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 45, 320, 30)];
searchBackView.image = [UIImage imageNamed:#"search1.gif"];
searchBackView.userInteractionEnabled=YES;
searchBackView.backgroundColor=[UIColor clearColor];
[tableHeader addSubview:searchBackView];
searchBar=[[[UISearchBar alloc] init] autorelease];
searchBar.delegate=self;
[searchBar setKeyboardType:UIKeyboardTypeDefault];
searchBar.barStyle = UISearchBarIconClear;
searchBar.frame=CGRectMake(17, 3, 285, 30);
[searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:#"search1.gif"] forState:UIControlStateNormal];
searchBar.backgroundColor=[UIColor clearColor];
[searchBackView addSubview:searchBar];
[searchBackView release];
[tableHeader addSubview:label];
for (UIView *subView in self.searchBar.subviews)
{
for (UIView *secondLevelSubview in subView.subviews){
if ([secondLevelSubview isKindOfClass:[UIImageView class]]){
[secondLevelSubview removeFromSuperview];
}
}
}
searchField = nil;
for (UIView *subView in self.searchBar.subviews)
{
for (UIView *secondLevelSubview in subView.subviews){
if ([secondLevelSubview isKindOfClass:[UITextField class]])
{
searchField = (UITextField *)secondLevelSubview;
break;
}
}
}
for (UIView *subview in searchBar.subviews)
{
for (UIView *secondLevelSubview in subview.subviews){
if ([secondLevelSubview conformsToProtocol:#protocol(UITextInputTraits)])
{
[(UITextField *)secondLevelSubview setClearButtonMode:UITextFieldViewModeNever];
}
}
}
if (searchField) {
searchField.leftViewMode = UITextFieldViewModeNever;
}
BluemenAppDelegate *delegate=(BluemenAppDelegate*)[[UIApplication sharedApplication] delegate];
if (isShowlist==YES) {
delegate.search_string = #"";
searchBar.text = delegate.search_string;
isShowlist = NO;
}
searchBar.text = delegate.search_string;
Update
Now I am able launch app but now if click on any textfield in app it is crashing and giving XPC connection interrupted error
If I understood correctly, you're trying to achieve a SearchBar with a clear background. As seen on the error message, iOS 13 won't allow UISearchBarBackground to be removed. There is a work around you can try:
This will make the search background image to be transparent, without removing it.
for (UIView *subView in self.searchBar.subviews) {
for (UIView *secondLevelSubview in subView.subviews){
if ([secondLevelSubview isKindOfClass: [UIImageView class]]) {
secondLevelSubview.alpha = 0.0;
break;
}
}
}
Just replace this part of your code with this one and check if it attends what you are expecting.

How do you set a different font colour for each item in a UIActionSheet

I know how to change the font colour of a UIActionSheet using a tint
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
[[UIView appearanceWhenContainedIn:[UIAlertController class], nil] setTintColor:[[ColorUtil alloc] colorWithHexString:#"25A559"]];
}
How do I apply it to an item. EG. The first item red, the second item green.
Check my answer
#pragma mark - changeImage
- (IBAction)myActionSheet:(id)sender
{
UIActionSheet *actionSheet=[[UIActionSheet alloc] initWithTitle : #"Choose Image" delegate : self cancelButtonTitle :#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Gallery",#"Camera",nil];
actionSheet.tag = 1000;
[actionSheet showInView :self.view];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
UIAlertController *alertController = [actionSheet valueForKey:#"_alertController"];
if ([alertController isKindOfClass:[UIAlertController class]])
{
alertController.view.tintColor = [UIColor redColor];// set your color here
}
}
}
and for iOS version less than 8.0, use the same old code of UIActionSheet delegate method:
#pragma mark - actionsheet delgates
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
for (UIView *subview in actionSheet.subviews) {
if ([subview isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)subview;
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; // set your color
}
}
}

UiActionSheet bug in iOS

i wanted to change buttons color in action sheet so i wrote this code
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
for (UIView *_currentView in actionSheet.subviews) {
if ([_currentView isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)_currentView;
button.titleLabel.textColor = [UIColor darkGrayColor];
NSLog(#"%#",#"ahmed");
}
}
}
but nothing happen although i made delegate to self and called UIActionSheetDelegate .
although i have 6 buttons ,i see as this function wasn't be called at all
what is the problem?????

Customize searchbar searchIcon

I am customizing my search bar with this but it is not showing any image. It remove its default image as well.
UITextField *searchField = nil;
for (UIView *subview in [[workLocationSearchBar.subviews lastObject] subviews]) {
NSLog(#"hey");
if ([subview isKindOfClass:[UITextField class]]) {
searchField = (UITextField *)subview;
NSLog(#"inside");
break;
}
}
if (searchField) {
UIView *searchIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"circle.png"]];
if ([searchIcon isKindOfClass:[UIImageView class]]) {
}
NSLog(#"aye");
searchField.rightView = searchIcon;
searchField.leftView=searchIcon;
}
Try This:
[workLocationSearchBar setImage:[UIImage imageNamed:#"IMAGE_NAME"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];

Alertview: alertViewShouldEnableFirstOtherButton method can't detect subview textView

I create a custom alertview that subview a uitextview inside it, as shown in the screenshot.
alertview with subviewed textview
I want to disable the Submit button when the textview is empty, so I edited the alertViewShouldEnableFirstOtherButton method:
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
if ( alertView.tag == 5000 ) {
for(UIView *view in alertView.subviews) {
if([view isKindOfClass:[UITextView class]]) {
UITextView *textField = (UITextView *)view;
if ( textField.text.length == 0 ) {
return NO ;
}
return YES ;
}
}
}
else {
return YES ;
}
}
Initially, the Submit button is disable which is right, but after I edit (type something on the textview), the submit button is still disabled, it seems that the alertViewShouldEnableFirstOtherButton only call once.
So, can someone teach me how to enable the submit button after I type in something on the subviewed textview? Thanks for the great help.
##########First edit
Below is the full code about the alertview,
- (void)addRemarkAlert {
alert = [[UIAlertView alloc]
initWithTitle:[NSString stringWithFormat:#"Remarks\n\n\n\n\n\n"]
message:nil
delegate:self
cancelButtonTitle: #"Cancel"
otherButtonTitles:#"Submit", nil ];
// textView to subview in the alertview
textView_onAlertView = [[UITextView alloc]initWithFrame:CGRectMake(12, 50, 260, 100)];
textView_onAlertView.delegate = self ;
textView_onAlertView.layer.borderWidth = 2.0f;
textView_onAlertView.layer.borderColor = [[UIColor darkGrayColor] CGColor];
textView_onAlertView.layer.cornerRadius = 10.0f;
textView_onAlertView.clipsToBounds = YES ;
textView_onAlertView.autocorrectionType = UITextAutocorrectionTypeNo ;
textView_onAlertView.autocapitalizationType = UITextAutocapitalizationTypeSentences ;
textView_onAlertView.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:12];
[alert addSubview:textView_onAlertView];
alert.tag = 5000 ;
[alert show];
}
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
if ( alertView.tag == 5000 ) {
for(UIView *view in alertView.subviews)
{
if([view isKindOfClass:[UIButton class]])
{
UIButton *aButton = (UIButton *)view;
if([aButton.titleLabel.text isEqualToString:#"Submit"])
{
if ( textView_onAlertView.text.length> 0 )
{
((UIButton *) view).enabled = YES;
}
else
{
((UIButton *) view).enabled = NO;
}
}
}
}
return NO;
}
else {
return YES ;
}
}
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
NSLog(#"%d",tv.text.length);
for(UIView *view in alertView.subviews)
{
if([view isKindOfClass:[UIButton class]])
{
UIButton *aButton = (UIButton *)view;
if([aButton.titleLabel.text isEqualToString:#"done"])
{
if ( tv.text.length> 0 )
{
((UIButton *) view).enabled = YES;
}
else
{
((UIButton *) view).enabled = NO;
}
}
}
}
return NO;
}
//////////////////////////////////////Entire code for your question
- (void)viewDidLoad
{
[super viewDidLoad];
[self addRemarkAlert];
}
- (void)addRemarkAlert
{
alert = [[UIAlertView alloc]
initWithTitle:[NSString stringWithFormat:#"Remarks\n\n\n\n\n\n"]
message:nil
delegate:self
cancelButtonTitle: #"Cancel"
otherButtonTitles:#"Submit", nil ];
// textView to subview in the alertview
textView_onAlertView = [[UITextView alloc]initWithFrame:CGRectMake(12, 50, 260, 100)];
textView_onAlertView.delegate = self ;
textView_onAlertView.layer.borderWidth = 2.0f;
textView_onAlertView.layer.borderColor = [[UIColor darkGrayColor] CGColor];
textView_onAlertView.layer.cornerRadius = 10.0f;
textView_onAlertView.clipsToBounds = YES ;
textView_onAlertView.autocorrectionType = UITextAutocorrectionTypeNo ;
textView_onAlertView.autocapitalizationType = UITextAutocapitalizationTypeSentences ;
textView_onAlertView.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:12];
[alert addSubview:textView_onAlertView];
alert.tag = 5000 ;
[alert show];
}
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
if ( alertView.tag == 5000 ) {
for(UIView *view in alertView.subviews)
{
if([view isKindOfClass:[UIButton class]])
{
UIButton *aButton = (UIButton *)view;
if([aButton.titleLabel.text isEqualToString:#"Submit"])
{
if ( textView_onAlertView.text.length> 0 )
{
((UIButton *) view).enabled = YES;
}
else
{
((UIButton *) view).enabled = NO;
}
}
}
}
return NO;
}
else
{
return YES ;
}
}
- (void)textViewDidChange:(UITextView *)textView
{
[self alertViewShouldEnableFirstOtherButton:alert];
}

Resources