0

enter image description here i have two Viewcontroller. one with tableview,check button, add button at bottom . i just adjust my tableview up and kept my add button at bottom. when user press my add button it will take to next viewcontroller ( i did these thing via storyboard )

Needed:

I need my add button should be bottom to above my table view.when user scroll down my table view also it should stick at centre of my tableview.i have tried with creating seperate view ,but no use can't do that.Here this is my viewcontroller.m file:

Thanks in advance !

I used storyboard ,so i did iboutlet and synthesis it,

@interface ViewController ()



@property (strong) NSMutableArray *notes;
@end

@implementation ViewController
@synthesize tableView;
@synthesize addButton;

my viewdidload:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.title = @"My Notes";

    tableView.dataSource = self;
    tableView.delegate = self;
    [self.view addSubview:tableView];
   }

when my add button presses it will move to another viewcontroller:

- (IBAction)addButtonPressed:(id)sender {
    AddNoteViewController *addNoteVC = [AddNoteViewController new];




    // to remove unused warning....
#pragma unused (addNoteVC)

}

Like this i need but in centre ....

david
  • 636
  • 2
  • 12
  • 29
  • Sorry I'm a bit confused about what you're trying to do – Peter Willsey Sep 04 '15 at 21:49
  • What do you mean by "when user scroll down my table view also it should stick at centre of my tableview" and "Like this i need but in centre"? – Aaron Brager Sep 04 '15 at 21:56
  • Seems like you just want a button on top of your table view, add it to your storyboard but make sure it's not inside the table view, and higher in the view hierarchy so it is not obscured by the table view. – Peter Willsey Sep 04 '15 at 22:10
  • in above example image add button in left .i need it in centre.at above of my table view (i.e hierarchy view) – david Sep 04 '15 at 22:19
  • please see this link https://camo.githubusercontent.com/eb4b9d9cc51e30254b575cd5b388ad57d0a4bace/687474703a2f2f692e696d6775722e636f6d2f7968344d7743422e676966 – david Sep 04 '15 at 22:20
  • like this i need my button in centre.when user scroll down my add button should be as it is? – david Sep 04 '15 at 22:22
  • did u understand what is my problem @peter – david Sep 04 '15 at 22:28
  • That's going to get complicated, you'll need to add it to your table view's scroll view, and you're going to have to set your view controller as the scroll view delegate. You will probably have to override scrollViewDidScroll and change the frame of your action button based on where the current content offset is. – Peter Willsey Sep 04 '15 at 22:31
  • if that's not possible. Then just adding a uibutton(bottom ) to tableview is possible...like above image – david Sep 04 '15 at 22:36
  • Do you want the UIButton to hover over the tableview or just appear as the user scrolls to the bottom? – MasterRazer Sep 04 '15 at 23:26
  • want that uibutton to hover over only.... – david Sep 04 '15 at 23:31
  • Possible duplicate of [How to put buttons over UITableView which won't scroll with table in iOS](http://stackoverflow.com/questions/14689805/how-to-put-buttons-over-uitableview-which-wont-scroll-with-table-in-ios) – Peter Lapisu Oct 27 '16 at 15:09

2 Answers2

2

Since you only want the UIButton to hover over your UITableView the solution should be quite easy.

I just created a UIButton which could the one you using.

in your method where you initialise the UIButton (e.g. viewDidLoad)

    yourBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [yourBtn setImage:[UIImage imageNamed:@"yourIMG"] forState:UIControlStateNormal];
    [yourBtn setTitle:@"+" forState:UIControlStateNormal];
    yourBtn.frame = CGRectMake(0, self.view.bounds.size.height -150, buttonwidth, buttonheight);
    yourBtn.center = CGPointMake(self.view.center.x, self.view.bounds.size.height -155);
    [yourBtn addTarget:self action:@selector(addButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:yourBtn];

If this shouldn't be your solution feel free to comment this answer!

Edit

To set the width and buttonheight just add the following. Write it above the yourBtn.frame line!

CGFloat buttonwidth = 57.5;
CGFloat buttonheight = 57.5;

Edit 2.0

You need to set the segues identifier first in IB. Check: iOS and xcode: how to give a segue a "storyboard id" so that I can programmatically manipulate it

-(IBAction)addButtonPressed:(id)sender {
    [self performSegueWithIdentifier:yourSegue sender:self];
}

Cheers

Community
  • 1
  • 1
MasterRazer
  • 1,377
  • 3
  • 16
  • 40
  • thanks will check out this....but getting error in this two line: addBtn.frame = CGRectMake(0, self.view.bounds.size.height -150, buttonwidth, buttonheight); error: ` use undeclared identifier buttonwidth, button height` – david Sep 05 '15 at 07:49
  • yeah, that's correct!! I added those to let you customize the size of the button. Just replace both with the desired sizes of the buttom you want. – MasterRazer Sep 06 '15 at 07:19
  • @ masterRazer yes , thanks its working....hoe to perform segue for this button.... – david Sep 06 '15 at 10:54
0

I think instead of frame setting in @MasterRazer's answer you should edit or add NSLayoutAttributeCenterX of your UIButton in IB. Setting that constraint of your button to 0 would make your button stays in the middle and be a clean and good solution for your problem.

Sabrican Ozan
  • 738
  • 3
  • 9
  • I didn't say btn.center can't be considered as a good solution, setting frame of the button could be considered as bad solution. Using autolayout with UI components will give you flexibilty about resizing and orientation changes. For this particular problem your solution can work but considering full cycle of development I don't think this is a perfect answer. @MasterRazer – Sabrican Ozan Sep 06 '15 at 12:35