iOS Scope Bar does not appear after added under UISearchBar - ios

I added scope bar under my search bar. However, scope bar does not appear. Any idea? Please help.
- (UISearchBar *)searchBar{
if (!_searchBar) {
_searchBar=[[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 24)];
[_searchBar setBackgroundImage:[UIImage imageNamed:#"ic_searchBar_bgImage"]];
[_searchBar sizeToFit];
[_searchBar setPlaceholder:#"Search"];
[_searchBar.layer setBorderWidth:0.5];
_searchBar.showsScopeBar = YES;
_searchBar.scopeButtonTitles = [NSArray arrayWithObjects:#"All", #"Near Me", #"Coming Soon", nil];
[_searchBar.layer setBorderColor:[UIColor colorWithRed:229.0/255 green:229.0/255 blue:229.0/255 alpha:1].CGColor];
[_searchBar setDelegate:self];
[_searchBar setKeyboardType:UIKeyboardTypeDefault];
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:#{
NSFontAttributeName: [UIFont fontWithName:#"Bitter" size:14],
}];
}
return _searchBar;
}
EDITED:
I added it in Tableview header. How can I change in order to make it appear?
- (UITableView *)tableView{
if (!_tableView) {
_tableView=[[UITableView alloc]initWithFrame:CGRectMake(0.0, 0.0, kScreenWidth, kScreenHeight-49.0) style:UITableViewStylePlain];
[_tableView setDelegate:self];
[_tableView setDataSource:self];
_tableView.tableHeaderView=self.searchBar;
UIView *v = [[UIView alloc] initWithFrame:CGRectZero];
[_tableView setTableFooterView:v];
}
return _tableView;
}

I tried to run your code and scope bar shows up fine for me.
So, while I cannot fathom from code why it should not appear, I would enlist some debugging tips here to help you see what else could go wrong that can cause this problem.
While your app is in debug running mode (iOS simulator), try this:
XCode -> Debug Menu -> View Debugging
Press Capture View Hierarchy.
It should come up with a tree structure on the left hand side, showing your table view. On the right hand side it will show you screenshot of your views
On the left hand side tree, look for the UISearchbar.
Under its hierarchy, search for the object of type: _UISearchBarScopeBarBackground. It is followed by UISegmentedControl which has segments corresponding to each scope text.
Refer to below image (this should appear in your scope hierarchy):
If UISegmentedControl appears fine here but not visible on the right side, check to see if its hidden by some other view / table view itself. Some frame adjustment should fix this issue.
If UISegmentedControl does not appear on the left side itself, you seem to have serious problem. Probably your outlets are not initialised and you may want to check if your table view is properly initialised and all. In any case this is very less likely to occur and it's only possible if nothing at all was visible on your simulator screen.

Related

Appearance Proxy overridden when resetting text

I am setting up my UI by using the UIAppearance Proxy. I setup my UITextfields to have a custom font like so:
[[UILabel appearanceWhenContainedIn:[RDTextField class], nil] setTextColor:[UIColor rds_purple]];
[[UILabel appearanceWhenContainedIn:[RDTextField class], nil] setFont:[UIFont rds_ralewayWithSize:20.0]];
This works perfectly, and when my textfields appear on screen they have the right font. However I enable my user to shuffle the strings that are in the textfields like this:
- (IBAction)shuffleValuesButtonPressed:(id)sender {
self.randomStringTextfield.text = [self randomString];
}
When this happens the font goes from being my custom font to being the default black font. Why is this happening, and what is the solution to get my new string to be in my custom font that I setup in the appearance proxy?
According to the Apple developer docs:
iOS applies appearance changes when a view enters a window, it doesn’t change the appearance of a view that’s already in a window. To change the appearance of a view that’s currently in a window, remove the view from the view hierarchy and then put it back.
So this solution works by removing everything and adding it back, but there is probably a better way:
- (IBAction)shuffleValuesButtonPressed:(id)sender {
self.randomStringTextfield.text = [self randomString];
[self refreshViews];
}
- (void)refreshViews {
for (UIWindow *window in [UIApplication sharedApplication].windows) {
for (UIView *view in window.subviews) {
[view removeFromSuperview];
[window addSubview:view];
}
}
}

IOS 7 - Search Display Controller in TableView White Scrollview Background - Cannot change background color

In my app I'm using dark table and cells. I've set everything to black in my storyboard, and when it all works perfect until I add a Search Display Controller. The search box itself is the desired color, but when I pull down, I see a scroll view that is white. I cannot find anywhere in the storyboard settings to change that background color. I've checked several places to include searching this site in depth. Any help would be greatly appreciated.
I've gone as far as calling this helper I found on stackoverflow from viewDidLoad and programmatically setting all backgrounds to black. It still shows up as white. I've tried this for the self.view self.tableView self.searchDisplayController and still no dice.
- (void)listSubviewsOfView:(UIView *)view {
// Get the subviews of the view
NSArray *subviews = [view subviews];
[view setBackgroundColor:[UIColor blackColor]];
for (UIView *subview in subviews)
{
[subview setBackgroundColor:[UIColor blackColor]];
NSLog(#"%#, %#", [subview class], subview.backgroundColor);
[self listSubviewsOfView:subview];
}
}
Accidentally I found a solution to this problem. If you set the backgroundView property of the table view to anything, for example:
self.tableView.backgroundView = [[UIView alloc] init];
the white background disappears and backgroundColor is visible as expected.
Use this method .. it will call when you start searching..
- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
{
tableView.backgroundColor = [UIColor anyColor];
}
You need to remember that while searching, another table view is loaded by the searchDisplayController.
So you may need to set the backgroundColor of the searchResultsTableView, like this:
[self.searchDisplayController.searchResultsTableView setBackgroundColor:[UIColor purpleColor]]; //Purple couldn't be a good choice :)

Strange behavior with UIToolbar in iOS app

In my current project I have an app based on a navigation controller. Some of the view in the app require a toolbar at the bottom of the view. I am creating the toolbar programmatically with the following (excerpted from viewDidLoad in a table view controller):
UIBarButtonItem *reloadButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refreshList)];
[reloadButton setTitle:#"Refresh"];
[reloadButton setTintColor:[UIColor whiteColor]];
self.navigationController.toolbar.barStyle = UIBarStyleBlackTranslucent;
[self.navigationController.toolbar sizeToFit];
CGFloat toolbarHeight = [self.navigationController.toolbar frame].size.height;
[self.navigationController.toolbar setFrame:CGRectMake(CGRectGetMinX(self.view.bounds),
438,
CGRectGetWidth(self.view.bounds),
toolbarHeight)];
NSArray *toolbarItems = [[NSArray alloc] initWithObjects:reloadButton, nil];
[self setToolbarItems:toolbarItems];
[self.navigationController setToolbarHidden:NO];
This has worked well in most views. However, I am now incorporating it into another view using identical code. In that controller, the toolbar appears about an inch above the bottom of the view--it doesn't snap to the bottom of the view as it does in other view controllers. Once I have navigated to the problem view, the other toolbars in other view begin exhibiting the same behavior. This is only happening in the simulator, not on my physical device. However, I currently have only an iPhone 4 as my physical device. Is this a bug in the simulator or an indication of an issue somewhere? THanks!
You are setting the y position to a hardcoded value of 438. This will result in it not appearing at the bottom of the screen for the taller iPhone 5's). You should calculate the y position as well perhaps with:
CGRectGetMaxY(self.view.bounds) - toolbarHeight
this would result in:
[self.navigationController.toolbar setFrame:CGRectMake(CGRectGetMinX(self.view.bounds),
CGRectGetMaxY(self.view.bounds) - toolbarHeight,
CGRectGetWidth(self.view.bounds),
toolbarHeight)];
It could also have to do with the presence of a navigation bar and the status bar

display the view behind tableView (or scrollView)

For example:
there is a tableView, when it is pulled down, the back view display. the effect is like this:
I tried like this but not working:
[self.view addSubview:logoView];
[self.view addSubview:tableView];
You have to
add your logo image view below the table view in xib.
set table view background color to clear color
Set logo image hidden.
in - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate or check for your self which delegate is best suited. set the logo image hidden to NO
You set the tableView background as your logoview. Keep your UITableviewCell as what color you want.
I'm not sure if i recognize your problem but i think you want something like this:
you'll have to use:
[self.view insertSubview:aboveSubview: ];
[self.view insertSubview:belowSubview: ];
not
[self.view addSubview: ];
You can achieve the same as the example image above by adding the view with a negative y offset.
UIView *header = [[UIView alloc] initWithFrame: CGRectMake(0.0f, 0.0f - self.view.bounds.size.height, self.view.bounds.size.width, self.view.bounds.size.height)];
header.backgroundColor = [UIColor lightGrayColor];
[self.tableView addSubview:header];
However if you have content in this view, such as a logo, it will appear to be pulled down from the top, rather than behind the table, I don't know if this is the effect you want or not.

Place floating UIButton on top of UICollectionView

I'm desperately trying to place a UIButton on top of my UICollectionView. The goal is something like the check-in button from the new foursquare app.
My code currently looks like so:
UIButton *floatingButton = [[UIButton alloc] init];
[floatingButton setFrame:CGRectMake(320, 150, 50, 50)];
[floatingButton setBackgroundColor:[UIColor blackColor]];
[self.collectionView addSubview:floatingButton];
[self.collectionView bringSubviewToFront:floatingButton];
The problem is the button isn't anywhere.
you probably want to add the button to the collectionView's parent view.
most likely self.view, but its hard to say for sure without seeing more code.
instead of
[self.collectionView addSubview:floatingButton];
you probably want
[self.view addSubview:floatingButton];
I had the same problem. Add your button to collectionView superView, might be self.view.
[self.view addSubview:floatingButton];
and set the button as firstResponder.
[floatingButton becomeFirstResponder];

Resources