I want to use a custom UISearchBar with the UISearchController as navigation title to work around the issue with iOS 11 that the height changed from 44 to 56 points. But I fail to understand how I can set the custom search bar to the search controller - the searchbar property is read-only. All my searches on the topic didn't result in anthing that helps me get on the right path.
Here is my search controller code:
Note: AppSearchControllerViewController is a simple UITableViewController displaying the results via an NSMutableArray
Note 2: the view controller implements the delegate "UISearchBarDelegate"
-(void)viewDidLoad {
searchResultsController = [[AppSearchControllerViewController alloc] init];
appSearchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
[searchResultsController setParentNavigationController:self.navigationController];
appSearchController.searchResultsUpdater = self;
appSearchController.dimsBackgroundDuringPresentation = YES;
// Usually one would set up the searchbar like this
// self.navigationItem.titleView = appSearchController.searchBar;
// but instead I do this:
[self addSearchBar];
}
- (void)addSearchBar {
_searchBar = [self addSearchBarWithFrame:CGRectMake(0, 0, kScreenWidth - 2 * 44 - 2 * 15, 44)];
UIView *wrapView = [[UIView alloc] initWithFrame:_searchBar.frame];
[wrapView addSubview:_searchBar];
self.navigationItem.titleView = wrapView;
}
- (WMSearchBar *)addSearchBarWithFrame:(CGRect)frame {
self.definesPresentationContext = YES;
WMSearchBar *searchBar = [[WMSearchBar alloc]initWithFrame:frame];
searchBar.delegate = self;
searchBar.placeholder = [Language get:#"APP_SEARCH_NAVBAR_TEXTFIELD_SEARCH_PLACEHOLDER"];
[searchBar setShowsCancelButton:NO];
[searchBar setTintColor:KGenericColor];
if (self.isChangeSearchBarFrame) {
CGFloat height = searchBar.bounds.size.height;
CGFloat top = (height - 20.0) / 2.0;
CGFloat bottom = top;
searchBar.contentInset = UIEdgeInsetsMake(top, 0, bottom, 0);
}
return searchBar;
}
WMSearchBar class is from this Github repo: https://github.com/DreamTravelingLight/searchBarDemo
This make the search bar perfectly fit again to 44 points and also do the fade in/out animation on controller transitions.
The issue is now, that I receive all the events from the search bar - that all works. But the search results controller is not triggered so the results also don't display.
I tried also to set the original search bar's text from my custom new search bar, but same effect - results controller does not show up. What do I need to do to wire in the new search bar and replace the old one?
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
NSLog(#"textDidChange: %#", searchText);
appSearchController.searchBar.text = searchText;
...
I am trying to add UITabBarController programmatically. Everything is working fine but I am having two issues.
I am setting tabbar background image but it shows a different image which I dont even have in resources.
I am using this image as tabbar background image with a green line above :
But it shows another green line at bottom like this:
Here is the code I am using for this:
[self.myTabBarController.tabBar setBackgroundImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"tabbar.png" ofType:nil]]];
Another issue is, I am setting tabbar item images using this code:
MyViewController *myController = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
UINavigationController *myNavController = [[UINavigationController alloc] initWithRootViewController: myController];
[myNavController.tabBarItem initWithTitle:#"" image:[UIImage imageNamed:#"ads_inactive.png"] selectedImage:[UIImage imageNamed:#"ads_active.png"]];
Images are set but when i try to add title in MyViewController's viewDidLoad using this:
self.title = #"My Ads";
It shows same title on tabbar item too but I dont want any title there.
How I can fix this issue?
Thanks
The image size is less than the frame size of tab bar , so to cover to frame area background image show two times.
You can change it in two ways
1.)Change the image size that will be not the best option
2.) set the content insets of tabBar like (0,0,0,0)
Example:
UITabBar *tabBar = self.tabBarController.tabBar;
iterm0.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
UITabBarItem *iterm0 = [tabBar.items objectAtIndex:0];
[iterm0 setImage:[[UIImage imageNamed:#"tab1_normal"]
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[iterm0 setSelectedImage:[[UIImage imageNamed:#"tab1_selected"]
[(UITabBarItem *)[self.tabBarController.tabBar.items objectAtIndex:0] setTitle:#""];
I'm trying to create modalviewcontroller, which should look like this
My problem is in the close button. As you can see in this example it almost out of uiview.
I was tried uiview, who has close button, make transparent but when modalview opens it's still in light gray color.
There is bit of my code :
Settings *new = [[Settings alloc]init];
new.modalPresentationStyle = UIModalPresentationFormSheet;
//i was change to other presentation styles, but that no I want.
new.view.backgroundColor = [UIColor clearColor];
[self presentModalViewController:new animated:YES];
CGRect r = CGRectMake(self.view.center.x - 327,
self.view.center.y - 255,
630, 490);
r = [self.view convertRect:r toView:new.view.superview.superview];
new.view.superview.frame = r;
If you still don't understand, what I want there is ilustration of modal view I need:
What is your suggestion, how can I make it?
I'm trying to have a custom titleView for Iphone app iOS 5.0+ . So I placed a graphic like this :
UIView *customTitleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 104, 72)];
UIImageView *customTitleGraphics = [[UIImageView alloc] initWithImage:image];
[customTitleGraphics setTag:kNavigationImageView];
CGRect myFrame = customTitleGraphics.frame;
myFrame.origin.y = 15.0;
myFrame.origin.x = -3.0;
customTitleGraphics.frame = myFrame;
[customTitleView addSubview:customTitleGraphics];
navigationItem.titleView = customTitleView;
And It works so far. The problem i Have is when I try to push another viewController: It looks like the titleView get cut off suddenly when the animation starts.
https://www.dropbox.com/s/0ysiroxayfld1l3/titleview.mov?v=1mci
Do you have any idea where I'm wrong?
I think that the title view is resized by the UINavigationBar when you push the new VC to the navigation stack. What I would suggest would be to prevent the navigation view from clipping to bounds…
Basically what I am trying to figure out to do is, say I have one View Controller, called V1, that has a regular view inside it and a button. Now, when you tap that button, I want that button to create an action that pop-ups another View Controller, called V2, within the same view controller, V1.
V2 will be reduced in size some so that it does not fill the entire screen, but you can still see the first layer which is V1 behind V2. So basically, you never really leave V1. I hope this makes sense for what I'm trying to do. I know the MTV app has this functionity. An image of what I'm talking about is here: https://docs.google.com/leaf?id=0BzlCAVXRsIPcNWUxODM2MDAtNDE3OS00ZTc4LTk5N2MtZDA3NjFlM2IzNmZk&hl=en_US
Sample code or an example is what I'm looking for as well.
Thanks
You can create such view by setting appropriate property type of modalPresentationStyle. See my example below:
UIViewController *V2 = [[UIViewController alloc] init];
V2.modalPresentationStyle = UIModalPresentationFormSheet;
V2.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[V1 presentViewController:V2 animated:YES completion:nil];
V2.view.superview.frame = CGRectMake(0, 0, 540, 620); //it's important to do this after presentModalViewController
V2.view.superview.center = V1.view.center;
[V1 release];
Try this:
V2 *d = [[V2 alloc]initWithNibName:#"V2" bundle:nil];//assuming V2 is name of your nib as well
d.delegate = self; //Optional:you only need this if you want to delegate
//create popover and put V2 in the popover view
UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:d];
popoverController.delegate = self; //optional
CGSize size = CGSizeMake(325, 75); // size of view in popover…V2
popoverController.popoverContentSize = size;
[d release];
[popoverController presentPopoverFromRect:yourButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
If you want to present this as a modal popup in iOS 8 with a similar style to the OP's screenshot here's what I did:
UIViewController *V2 = [[UIViewController alloc] init]; // V2 is the popup
V2.modalPresentationStyle = UIModalPresentationFormSheet;
V2.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
V2.preferredContentSize = CGSizeMake(325, 75); // size of popup view
[V1 presentModalViewController:V2 animated:YES]; // V1 is the current topmost view controller
I like this better than using a UIPopover because you don't need to mess with arrow directions and the user cannot close it by tapping outside of the popup.
These properties can also be set in a storyboard/nib via the designer. To set preferredContentSize check "Use Preferred Explicit Size" and set the values.
This only works on the iPad.
There is a very good library to display a view controller as Popup on iPhone
see here https://github.com/martinjuhasz/MJPopupViewController
If you're using Storyboard, you can follow this step:
Add a view controller (V2), setup the UI the way you want it
*based on the image you attached
add an UIView - set background to black and opacity to 0.5
add an UIImageView - that will serve as your popup (Pls take note that the image and the view must not have the same level/hierarchy. Dont make the imageview the child of the view otherwise the opacity of the uiview will affect the uiImageView)
Present V2 Modally
Click the segue. In the Attributes inspector, Set Presentation as Over Full Screen. Remove animation if you like
Select V2. In the Attributes inspector, Set Presentation as Over Full Screen. Check Defines Context and Provides Context
Select the MainView of your V2 (Pls. Check image). Set backgroundColor to Clear Color
file .m ---> this is the implementation file
-(IBAction)anyAlert:(id)sender{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"A Message" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK!", #"Other Title", nil];
[alert show];
[alert release];
}
remember declare
-(IBAction)anyAlert:(id)sender;
in the file .h ---> header file
It works for me, hopefully for you...
Create UIView for v2 and add in v1.
- (void)viewDidLoad
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];
}
- (void)aMethod:(id)sender
{
CGRect * imageFrame = CGRectMake(10, 90, 300, 300);
V2 *v2 = [[V2 alloc] initWithFrame:imageFrame];
[self.view addSubview:v2];
}