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

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.

Related

Finding all UIButtons in subviews

I have a single UIButton in the view of my UIViewController. I also have ten more that are in a subview in the main view. I want to find all these buttons. So far I have:
-(void)findAllButtons{
for(UIView *view in self.view.subviews) {
if ([view isKindOfClass:[myButton class]]){
NSLog(#"found a button!");
}
}
}
It is only finding the single button though and not the other ten. Why is that? Shouldn't it iterate every single subview and then find them?
for (UIView *subView in scroll.subviews) {
if ([subView isKindOfClass:[UIButton class]]) {
UIButton *btn = (UIButton*)subView;
if (btn.tag == selectedButton.tag) {
btn.layer.borderWidth = 1.0f;
btn.layer.borderColor = [UIColor darkGrayColor].CGColor;
}else{
btn.layer.borderWidth = 1.0f;
btn.layer.borderColor = [UIColor clearColor].CGColor;
}
}
}
Just a few lines of code
-(void)findAllButtons {
[self findButtonsInSubviews:self.view.subviews];
}
- (void)findButtonsInSubviews:(NSArray *)subviews {
for(UIView *view in subviews) {
if ([view isKindOfClass:[UIButton class]]){
NSLog(#"found a button!");
} else {
[self findButtonsInSubviews:view.subviews];
}
}
}
A recursive function using Objective-C blocks like this will find all views of a given subclass type as specified in the test block in the view hierarchy of the given view:
NSMutableArray *marrAllButtons = [NSMutableArray new];
BOOL (^viewTest)(UIView*) = ^BOOL(UIView* viewToTest) {
return [view isKindOfClass:[UIButton class]];
};
void(^viewEnumerator)(UIView*) = ^(UIView* outerView){
for (UIView *view in outerView.subviews)
{
if (viewTest(view))
{
[marrAllButtons addObject:view];
}
else
{
viewEnumerator(view);
}
}
};
viewEnumerator(self.view);
NSLog(#"All Buttons %#", marrAllButtons);
- (NSMutableArray *)buttonsInView:(UIView *)view
{
NSArray *subviews = view.subviews;
NSMutableArray *buttons = [NSMutableArray array];
for (UIView *subview in subviews)
{
if ([subview isKindOfClass:[UILabel class]])
{
[buttons addObject:subview];
}
else if(subview.subviews)
{
[buttons addObjectsFromArray:[self buttonsInView:subview]];
}
}
return buttons;
}
Your method is right if all your button already have in self.view (main view).
Just set tag for all button to check and also make sure that all button are on the main view. I hope this will work.
-(void)findAllButtons{
for(UIView *view in self.view.subviews) {
if ([view isKindOfClass:[myButton class]]){
UIButton *button = (UIButton*)view;
NSLog(#"found a button with tag:%d",button.tag);
}
}
}
for(UIView * subView in view.subviews) // here write Name of you ScrollView.
{
// NSLog(#"test %#", [subView class]);
if([subView isKindOfClass:[UIButton class]])
{
UIButton *button = (UIButton*)subView;
[button setSelected:NO] ;
NSString *s1;
s1 = #",";
s1 = [s1 stringByAppendingString:[NSString stringWithFormat:#"%#",button.titleLabel.text ]];
s1 = [s1 stringByAppendingString:[NSString stringWithFormat:#"%#",#"," ]];
NSRange range = [temp_Colors_Name_comma rangeOfString:s1 ];
if(range.location == NSNotFound)
{
}
else
{
[button setSelected:YES];
}
}
}

Disable interaction for views behind background image

To show a help view I've put an UIImageView which behind has some UIButtons. How can I disable user interaction of these buttons? If I touch this image where buttons are behind, they responds to touch events.
CODE FOR IMAGE BACKGROUND:
self.helpBackground = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
self.helpBackground.backgroundColor = [UIColor colorWithWhite:0 alpha:0.75];
self.helpBackground.hidden = YES;
[self.view addSubview:self.helpBackground];
I've used self.helpBackground.userInteractionEnabled = NO; but didn't work.
Thanks.
Put your buttons in a array and loop through them and disable them:
NSArray *buttonsArray = #[yourButton1, yourButton2];
for (UIButton *button in buttonsArray) {
button.enabled = NO;
}
And when you want them enabled just loop again and set enabled to YES
Keep a Tag to your helpView(imageview) and add the following code
for (UIView *view in self.view.subviews) {
if (view.tag != yourViewTag) {
view.userInteractionEnabled = NO;
}
}
And after removing the help screen use the following code
for (UIView *view in self.view.subviews) {
view.userInteractionEnabled = YES;
}
you can try below solution..When help imageview is appearing
for (UIView *view in [self.view subviews])
{
if (view isKindOfClass:[UIButton Class])
{
view.userInteractionEnabled = NO;
}
else
{
view.userInteractionEnabled = YES;
}
}
////After dismissing the help screen you can
for (UIView *view in [self.view subviews])
{
if (view isKindOfClass:[UIButton Class])
{
view.userInteractionEnabled = YES;
}
}
////(OR) Simply do as below
self.view.userInteractionEnabled=YES;
Hope it helps you..
You can create a method that disables user interaction for all views that are under your helpBackground:
- (void)disableUserInteractionForViewsUnderView:(UIView *)view
{
CGRect area = view.frame;
for (UIView *underView in [self.view subviews]) {
if(CGRectContainsRect(area, underView.frame))
underView.userInteractionEnabled = NO;
}
}
and after that you call it where you need it:
[self disableUserInteractionForViewsUnderView:self.helpBackground];
EDIT
I've created a UIViewController category gist on github.com. You can find it here: https://gist.github.com/alexcristea/0244b50e503e8bf4f25d
You can use it like this:
[self enumerateViewsPlacedUnderView:self.helpBackground usingBlock:^(UIView *view) {
if ([view isKindOfClass:[UIButton class]]) {
view.userInteractionEnabled = NO;
}
}];

How to set UIActionSheet button color

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

Rectangular UISearchBar on iOS 7

I'm trying to make a UISearchBar rectangular instead of rounded, but all the solutions I found so far (mostly iterating through subviews) seem broken on iOS 7.
I did some research myself and as it turns out, it only has a UIView subview, which has additional subviews, a UISearchBarBackground and a UISearchBarTextField (both of them are private classes).
I tried
if ([view isKindOfClass:NSClassFromString(#"UISearchBarBackground")]) {
[view removeFromSuperview];
}
and
if ([view conformsToProtocol:#protocol(UITextInputTraits)]) {
#try {
[(UITextField *)view setBorderStyle:UITextBorderStyleRoundedRect];
}
#catch (NSException * e) {
// ignore exception
}
}
where view is the subview of that one UIView subview but none of them seems to work.
Try this... (I know it is also using subview but it is working in ios7)
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 20, 320, 49)];
[self.view addSubview:searchBar];
[self checkSubViewsOfView:searchBar WithTabIndex:#""];
and Add this method
-(void)checkSubViewsOfView:(UIView *)view WithTabIndex:(NSString *)strTab
{
if ([view isKindOfClass:NSClassFromString(#"UISearchBarTextField")])
{
view.layer.borderWidth = 1;
view.layer.borderColor = [[UIColor whiteColor] CGColor];
return;
}
for (UIView *vvv in view.subviews)
{
NSLog(#"%#%#",strTab,[vvv description]);
if (vvv.subviews > 0)
{
NSString *str = [NSString stringWithFormat:#"____%#",strTab];
[self checkSubViewsOfView:vvv WithTabIndex:str];
}
}
}
you can set the searchfield-background like this:
[self.searchBar setSearchFieldBackgroundImage:[[UIImage imageNamed:#"searchbar_stretch-0-10-0-10"]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10)] forState:UIControlStateNormal];
and the searchbar-background like this:
[self.searchBar setBackgroundImage:[UIImage imageNamed:#"categories_navbar"]];

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

Resources