iOS floating button over table view [duplicate] - ios

This question already has answers here:
How to put buttons over UITableView which won't scroll with table in iOS
(10 answers)
Closed 9 years ago.
Is it possible to add a small floating 'settings' button or image-button over a table view? I have buttons on a navigation bar up top, and i dont want to crowd that or remove any

This is possible by adding it to the main window as a subview. Another option and my preferred method: Have a main wrapper view, the table view can be added to that wrapper view as a subview, then add your button as another subview inside of the wrapper view and voila

Perhaps adding a toolbar with buttons at the bottom would be a better approach?

To use this method, you should inherit UIViewController and add tableview in it
(1)add button to self.tableview in viewDidLoad()
button.frame = CGRectMake(0, 0, 50, 50);
[self.tableview addsubview:button];
[self.tableview bringSubviewToFront:button];
(2)set tableview delegate
self.tableview.delegate = self;
(3) add and update button location to self.tableview in scrollViewDidScroll delegate
button.frame = CGRectMake(0, 0, 50, 50);
[self.view addSubview:button];
[self.view bringSubviewToFront:button];
voila~

Related

Creating UIView in UITableViewController that doesn't moves with the table view

I would like to display a small UIView with some text in the center of a UITableViewController when I call the showHud method.
- (void) showHud {
UIView *paintView=[[UIView alloc]initWithFrame:CGRectMake(150, 150, 120, 30)];
[paintView setBackgroundColor:[UIColor yellowColor]];
[self.view addSubview:paintView];
}
Actually the problem with this code is that the view doesn't stays on its place, when I scroll the tableview it goes up with cells. Is it possible to pin it somehow?
I don't think you can achieve that with a UITableViewController because the view property of such controller is a UITableView, so the content will always scroll with it.
You need to create a regular UIViewController and add a UITableView to that controller's main view. Then anything else you add to that main view, will stay in place as it is outside of the table view.
Try implementing scrollViewDidScroll delegate method and change the frame of paintView according to tableview offset.
eg:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGPoint offset = scrollView.contentOffset
[paintView setFrame:CGRectMake(150, 150+offset.y, 120, 30)];
}
But the best way would be to create a simple UIViewController.
You must use UItableViewController because of pullToRefresh controller?
UI could add a view to the window and position it to the very front:
UIView testView = [[UIIView alloc]initWithFrame:CGRectMake(10, [[UIScreen mainScreen]applicationFrame].size.height - 85 , 75, 75)]; [[[[[NSApplication sharedApplication] mainWindow] contentView] addSubview:testbox]; addSubview: testView]; testView.layer.zPosition = MAXFLOAT;
An UITableViewController is typically embedded in a "parent" view controller, like UINavigationController. Its UITableView will then have a superview, like UINavigationControllerWrapperView. Even if your table view controller is a root view controller, the table view's superview is UIWindow.
So you can safely assume that your table view always has a superview. Then add your UIView to this superview instead of the table itself:
//Note: frame should be different here as it's in the superview's coordinate system
UIView *paintView=[[UIView alloc]initWithFrame:CGRectMake(150, 150, 120, 30)];
[paintView setBackgroundColor:[UIColor yellowColor]];
[self.view.superview addSubview:paintView];

Table View Hide Searchbar until scrolled

I got a table view set up, and a working search bar - NO XIB OR STORYBOARD involved. Above the search bar I have label showing the numbers of entries in the table view and some other stuff. Well now I want the search bar + label hidden until the user scrolls up (like in Music App). This is the setup of my search bar
self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
self.searchBar.showsCancelButton = YES;
[self.view addSubview:self.searchBar];
self.searchBar.delegate = self;
self.tableView.contentInset = UIEdgeInsetsMake(self.searchBar.frame.size.height,0, 0, 0);
[self.tableView setTableHeaderView:self.searchBar];
[self.tableView setContentOffset:CGPointMake(0,88) animated:YES];
[self.zsearchDisplayController setActive:NO animated:YES];
This is my label:
tableCountDisplay = [[UILabel alloc]initWithFrame:CGRectMake(5, -44, 155, 44)];
The label is already hidden until the user scrolls. The problem is - I can't get the search bar to hide. If I do
[self.tableView setContentOffset:CGPointMake(0,88) animated:YES];
Then search bar and label are hidden but also the first element of my table view...
If I do 44 or 0 (doesn't matter which of them)
[self.tableView setContentOffset:CGPointMake(0,44 or 0) animated:YES];
the label is hidden, and everything else is visible. Technically 0,44 should be the right offset, but it does not work for some reason.
I'd be really happy about some help!
One of the solutions is to add your searchBar and your label as subViews to a UIView. Then set this UIView as the TableHeaderView. My working sample looks like this:
Screen before scrolling:
Screen after scrolling:
There is not a way to maintain the header of a tableView fixed
1- could use an UIViewController instead of UITableViewController.
2- add subview (UIView) for header (add the searchBarView in this view).
3- and add another subview for the tableview.
What about
tableView.contentOffset = CGPoint(x: 0, y: (tableView.tableHeaderView?.frame.size.height ?? 0))
on wiewWillAppear: ;)

Suggestions on how i can add 40 more "Round Rect Buttons" to my ScrollView [duplicate]

I want to have several buttons and other objects in a long UIScrollView in my app. In storyboard, I added a UIScrollView to fill the entre view, and then created an IBOutlet in my .h file. I synthesized the scroller in my .m file, and then used the following code to start the scroller:
#synthesize scroller = _scroller;
- (void)viewDidLoad
{
[super viewDidLoad];
[_scroller setScrollEnabled:YES];
[_scroller setContentSize:CGSizeMake(640, 3000)];
}
So now I need to know how to actually add things, such as buttons, to the area of the scroller that extends below what you can see in the view. My problem is that as I add butons to my view in storyboard, I can only add things to what you can see in the view, and therefore need to know how to add buttons to part that I will scroll to!
Hopefully this is clear. Thanks for all your help!
UPDATE
I have posted a screencast that walks through this technique step-by-step.
ORIGINAL
The easiest way to handle this is simply to make the view in your storyboard taller. When the app runs, any of the normal container view controllers (UINavigationController, UITabBarController, UISplitViewController, or even UIViewController when it's the root view controller of its window) will resize the view to fit on the screen and scroll.
Here's an example of how to set it up in Xcode:
I changed the view controller's size from “Inferred” to “Freeform”. Then I changed its view's height from 460 to 800. (By the way, control-shift-click gives you a menu of all objects under the cursor.)
Here's what happens when I run it in the simulator:
As you can see, the view hierarchy was resized to fit the screen, but the subviews of the UIScrollView weren't repositioned, and the scroll view set its content size appropriately. (That may only work properly with autolayout, though...)
So, an example to you how to add a Button:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTitle:#"Cool title" forState:UIControlStateNormal];
[btn setFrame:CGRectMake(50, 700, 100, 100)];
[btn addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
[_scroller addSubview:btn];
You just need to set the frame of the view that you want to add and after that add it as a subview in your scroll.
You can you use the Size inspector(Left part of xcode) in order to position it. or reposition it programmatically.
Cheers,
Kel

Add a button to float on top of a ViewController, Path inspired

I am trying to build a button that will be floating on top of a ViewController, it should remain in the same position statically while the viewcontroller beneath it can scroll up and down.
I designed my own button class and in the viewdidload of the parent Viewcontroller I am doing this :
- (void)viewDidLoad// this is the parent viewcontroller
{
[super viewDidLoad];
customButton *floatingButton = [[UIButton alloc]initWithFrame:(self.view.frame)];
[floatingButton setLocation:CGPointMake(300, 430)];
[self.view addSubview:floatingButton];
[self.view bringSubviewToFront:floatingButton];
This is not doing it, as in when I run the button doesn't show up, only the view controller.
How can I achieve this?
I don't think that you're initializing your button properly. initWithFrame is a UIView initializer, being called by a regular UIButton. I don't see how you would get anything but an empty view here.
If you have a custom initializer for your custom button you should use
[[CustomButton alloc] myCustomInitializerWithFrame:frame];
Thanks guys, but the error was on my end, I wasn't initializing the frame properly, so the button was there but had a height and width of (0,0).

UIView appearing before navigation transition is over

I'm writing an iPhone app with a table inside a navigation controller. When the user clicks one of the cells in the main screen a UIView on top of the incoming view controller is created (it's like a toolbar).
self.toolbar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];
toolbar.backgroundColor = [UIColor colorWithRed:0.8823 green:0.8823 blue:0.8823 alpha:1.0];
[self.navigationController.view addSubview:toolbar];
The problem is that the view appears before the transition to the new view controller is complete and the effect is pretty weird. I suppose this is due to the fact I add the view to the navigationController,but I need to do this otherwise the bar would scroll together with the table and instead I want it to be fixed.
Any suggestion?
I've found a possible solution: add the toolbar as TableHeaderView and follow iOS: Add UIView to UITableView
Any other better solution is more than welcome

Resources