I have an app in which I am creating a two views stacked on top of each other:
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, self.view.frame.size.height -200)];
[self.view addSubview:headerView];
[self.view addSubview:bottomView];
[headerView setBackgroundColor:[UIColor lightGrayColor]];
[bottomView setBackgroundColor:[UIColor redColor]];
I also use a sidemenu which uses:
[self.sideMenuViewController setContentViewController:[[UINavigationController alloc] initWithRootViewController:[self.storyboard instantiateViewControllerWithIdentifier:#"welcomeScreen"]]
animated:YES];
[self.sideMenuViewController hideMenuViewController];
The initial load is fine and both top and bottom views appear as expected, however, when I navigate away using the side menu and then navigate back to home using the sidemenu, both views appear to move up the page (behind the navigation bar).
I have tried everything I can think from programmatic constraints to checking that the Navigation bar is present and moving the code from viewDidLoad to viewDidAppear but I always get the same result. Any ideas as to what is happening and why? And any suggestions on a fix would be greatly appreciated. Thanks
if ([self respondsToSelector:#selector(setEdgesForExtendedLayout:)]) {
self.edgesForExtendedLayout = UIRectEdgeNone;
}
Has fixed the issue!
Related
Is there a way to insert any UIView in NavigationBar like the image below?
Is this not working?
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 20.0f, 320.0f, 32.0f)];
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
[[self view] addSubview: tempView];
[[self view] addSubview: navBar];
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle: #"Controls"];
[navBar pushNavigationItem:navItem animated:NO];
In the code below, I embed a view into the navbar, where my embedded view draws the background and the new buttons. The code also deals with relaying out the view when the device is rotated.
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
if (view.superview == nil) {
[self.navigationBar addSubview:view];
[self.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
}
CGRect frame = self.navigationBar.frame;
frame.size.height = MY_NAVBAR_HEIGHT;
self.navigationBar.frame = frame;
view.frame = self.navigationBar.bounds;
}
An alternative solution is to use a regular UIView as a NavBar, and by saying that what I mean is you have to set the UIView in navbar place and hide the NavigationController bar in the main VC and show it in the next one read this article Here https://medium.com/me/stats/post/54bc5fc56d6c
Hi in my application have i have popup view . I have made transparent background and I'm using UIView on top of it but in simulator its showing only half in the screen. like below images.
This is in my UIviewcontroller image
In simulator its showing like this.
Try changing your simulator to 3.5 Inch, because from what I see you haven't add autolayout to your Views to support 4 Inch.
Learn more about Autolayout
Most probably the registration controls (UILabel and UItextviews) are added on self.view instead on the view that is of half screen size.
You could go for the code below:
-(void)viewDidLoad
{
backgroundButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)]; //Background button to handle tap outside popup view
[backgroundButton addTarget:self action:#selector(tapDetected) forControlEvents:UIControlEventTouchUpInside];
}
-(void)tapDetected /* When user tap outside popup view so as to dismiss the view*/
{
[backgroundButton removeFromSuperview];
[tempView removeFromSuperview];
}
-(void)buttonClicked:(UIButton *)sender /*To create and open popup view */
{
[self.view addSubview:backgroundButton];
tempView = [[UIView alloc] initWithFrame:CGRectMake(15, 100, 300, 300)]; //PopUp View
[tempView setBackgroundColor:[UIColor redColor]];
[backgroundButton addSubview:tempView];
[tempView setAlpha:1];
}
Background: I have a UIViewController which has a UITableView added programmatically. At the top of the UITableView I have a tablHeaderView in which I have placed a UIView. This UIView has a UISearchBar and a segmentedControl. The idea being: that a user can sort the UITableView by some basic categories such as 'Date/Time' or 'Location' etc. They can also search by an item in the programme.
Problem: When I tap the search bar it resizes (which I don't want) and then when I cancel the search it resizes again and stays there until the UIViewController is exited and loaded again.
Code:
-(void)loadTableView
{
usableSpace = [[UIScreen mainScreen] applicationFrame];
usableWidth = usableSpace.size.width;
usableHeight = usableSpace.size.height;
_tableView = [[UITableView alloc] init];
[_tableView setFrame:CGRectMake(0,0,usableWidth, usableHeight)];
[_tableView setDataSource:self];
[_tableView setDelegate:self];
[self.view addSubview:_tableView];
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, usableWidth, 44)];
_searchBar.showsCancelButton = YES;
NSLog(#"searchBar height = %fl", _searchBar.frame.size.height);
segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Date/Time", #"Location", #"Speaker", nil]];
segmentedControl.frame = CGRectMake(0, (_searchBar.frame.size.height), usableWidth, (usableHeight * 0.075));
[segmentedControl addTarget:self action:#selector(sortList) forControlEvents:UIControlEventValueChanged];
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self;
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, usableWidth, (usableHeight * 0.15))];
[headerView addSubview:_searchBar];
[headerView addSubview:segmentedControl];
_tableView.tableHeaderView = headerView;
[self.view addSubview:_tableView];
self.tableView = _tableView;
}
What I have tried: I have tried setting the size of the SearchBar, not setting its size. Not having it in a UIView in the tableHeaderView, instead just having it there on its own.
Why does it resize at all and how can I get it to stop?
EDIT: I have just tried the following: In storyboard (where the UIViewController was originally created) I have selected the UIVC in question and in attributes inspector I deselected 'Under Top Bars' and 'Under Bottom Bars' and this appears to have fixed the first part of the animation problem. Now, when I tap in the search bar, the search becomes active but the searchBar does NOT resize. However, when I cancel the searchBar the searchBar still animates and resizes as per the last image in my screenshot. What could be causing THAT resizing?
i've been banging my head on this for too freakin long... props to this dude here for the clues.
if you want your UISearchBar to be in the header along with other views and to scroll with the header instead of sticking at the top, then you've got to remove it and re-add it after the search completes. to be clear, build your header like you already do and then throw this in there as well to handle the screwy animations.
-(void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {
[self.mySearchBar removeFromSuperview];
self.mySearchBar.frame = CGRectMake(0, 0, 320, 44);
[self.table.tableHeaderView addSubview:self.mySearchBar];
}
As Timothy Moose notes here, "It seems that UISearchDisplayController makes an undocumented assumption that the search bar is the only content of the header view"
...yay apple.
Ok, after hours of reading up and trying different things I think I have found out how to have a UISearchBar where I want it and it seems to work ok. I believe the problem was to do with either having multiple views in the tableHeaderView OR the UISearchBar did not like being with another view inside a 'containing' view.
Here is the code I ended up with that allowed me to have a segmentedControl at the top of my UITableView AND a UISearchBar which actually sat at the top of my UIView (to which the UITableView was added).
-(void)loadTableView
{
usableSpace = [[UIScreen mainScreen] applicationFrame];
usableWidth = usableSpace.size.width;
usableHeight = usableSpace.size.height;
_tableView = [[UITableView alloc] init];
[_tableView setFrame:CGRectMake(0,0,usableWidth, usableHeight)];
[_tableView setDataSource:self];
[_tableView setDelegate:self];
[self.view addSubview:_tableView];
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, usableWidth, 44)];
[_searchBar setShowsCancelButton:YES];
NSLog(#"searchBar height = %fl", _searchBar.frame.size.height);
segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Title", #"Date/Time", #"Speaker", nil]];
segmentedControl.frame = CGRectMake(0, (_searchBar.frame.size.height), usableWidth, (usableHeight * 0.075));
segmentedControl.selectedSegmentIndex = 0;
[segmentedControl addTarget:self action:#selector(sortList) forControlEvents:UIControlEventValueChanged];
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self;
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, usableWidth, (usableHeight * 0.15))];
[headerView addSubview:segmentedControl];
_tableView.tableHeaderView = headerView;
[self.view addSubview:_tableView];
[self.view addSubview:_searchBar];
self.tableView = _tableView;
}
This meant that I had:
UIViewController with a UINavigationController, UISearchBar, UITableView. The UITableView had a segmentedConrol. The UISearchBar sits just under the UINavigationController and is always present. The UISearchBar sits at the top of the UITableViewController and scrolls with the UITableViewCells.
If anyone knows why the UISearchBar acted as it did in the question then I would be grateful if they could leave a comment. I know that this problem (or very similar) has been experienced by MANY people and I haven't found a definitive answer anywhere.
I'm looking for a way to add a segmented control to my navigation bar, but I still want the title and bar buttons to be there.
Like the purchased section in the app store:
I have tried adding a bar segmented control to my navigation item, then using the prompt instead of the title, but the prompt does not have bold text. Could I make the text bold and still have bar buttons too?
I know you probably found another way, and that my answer may sound cheap, but here's what I did. I just added a view underneath my navigation bar with the proper constraints, and put my segmented control there. It works good for my application and looks pretty much like that picture.
Have a nice day.
give you the dirty example I made
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 64, 320, 60)];
view.backgroundColor = UIColorFromRGB(0xffffff);
[self.view addSubview:view];
UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 124, 320, 1)];
bottomView.backgroundColor = [UIColor grayColor];
[self.view addSubview:bottomView];
UISegmentedControl *sg = [[UISegmentedControl alloc] initWithItems:#[#"one", #"two"]];
sg.frame = CGRectMake(10, 10, 300, 40);
[view addSubview:sg];
for (UIView *view in self.navigationController.navigationBar.subviews) {
for (UIView *subView in view.subviews) {
[subView isKindOfClass:[UIImageView class]];
subView.hidden = YES;
}
}
the screenshot
hope this can enlighten you to achieve what you want
I am adding a UIDatepicker to a UIViewController, which is the rootview of a UINavigationController, and I use this code to position the Datepicker at the bottom of the screen.
UIDatePicker *picker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 216, 320, 216)];
[self.view addSubview:picker];
This should normally place the picker exactly at the bottom of the screen, but it is 44p missplaced. The Navigationbar is exactly 44p heigh, so I think this is the problem, but I don't know why the frame height includes the height of the navbar, when it doesn't act like a Subview.
I know a simple way would be to substract 44, but I am looking for a solution without any fixed numbers. Is there a way to implement it and can someone please explain me why the view includes the height of the navbar?
set the translucent property of navigation bar to YES to solve your problem..
Ex:
self.navigationControllerInstance.navigationBar.translucent = YES;
Well I think that since your UIViewController is the root view of a UINavigationController it's implicitly including the navigationBar on its bounds.
To get its frame programmatically, you could use:
CGRect navframe = [[self.navigationController navigationBar] frame];
UIDatePicker *picker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 216, 320, 216)];
[self.view addSubview:picker];
try this!
It's better not to hardcode any dimensions. Try this code:
UIDatePicker *picker = [[UIDatePicker alloc] init];
CGRect pickerFrame = CGRectMake(0, CGRectGetHeight(self.view.bounds) - CGRectGetHeight(picker.bounds), CGRectGetWidth(picker.bounds), CGRectGetHeight(picker.bounds));
[picker setFrame:pickerFrame];
[self.view addSubview:picker];
It works and does not assume that a datepickers height is always 216. That may change in future iOS implementations, so it is better to check the size at runtime.
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
I guess this will solve your problem!