1

我在UITableViewController. 它按预期工作。现在我想在UIViewController添加UITableView.

创建浮动按钮的代码相同,但行为不同。

UITableViewController按钮停留在它应该的地方。在UIViewController按钮向上移动(当我向下滚动时)和向下移动(当我向上滚动时)

如何让按钮停留在UIViewController. 喜欢上UITableViewController

表视图控制器.h

#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "SingletonClass.h"

@interface MagTableViewController : UITableViewController <UIGestureRecognizerDelegate>

@property (strong, nonatomic) IBOutlet UITableView *magTableView;
@property (strong, nonatomic) UIButton *oButton;
@property (nonatomic) float originalOrigin;

@end

TableViewController.m - 浮动按钮,按预期工作

- (void)viewDidLoad
{
    [super viewDidLoad];
    SingletonClass *sharedSingleton = [SingletonClass sharedInstance];
    self.originalOrigin = sharedSingleton.photoButtonY; // 430.0

    [self addOverlayButton];
}

#pragma mark Camera Button 
- (void)addOverlayButton {
    self.oButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.oButton.frame = CGRectMake(132.0, self.originalOrigin, 56.0, 56.0);
    [self.oButton setImage:[UIImage imageNamed:@"CameraButton56x56.png"] forState:UIControlStateNormal];
    [self.oButton addTarget:self action:@selector(toCameraView:) forControlEvents:UIControlEventTouchUpInside];

    [self.view insertSubview:self.oButton aboveSubview:self.view];
}

// to make the button float over the tableView including tableHeader
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGRect tableBounds = self.tableView.bounds; // should be _magTableView, but works with _tableView as well. But I do not know where this _tableView comes from...
    //CGRect tableBounds = self.magTableView.bounds;  

    // floating Button; 
    CGRect floatingButtonFrame = self.oButton.frame;
    floatingButtonFrame.origin.y = self.originalOrigin + tableBounds.origin.y;
    self.oButton.frame = floatingButtonFrame;

    [self.view bringSubviewToFront:self.oButton]; // float over the tableHeader
}

视图控制器.h

#import <UIKit/UIKit.h>
#import "SingletonClass.h"

@interface DetailViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) UIButton *oButton;
@property (nonatomic) float originalOrigin;

@end

ViewController.m - 跳跃按钮,不想要的行为(更新:不工作,不再跳跃)

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.originalOrigin = [[SingletonClass sharedInstance] photoButtonY];  // 430.0
    [self addOverlayButton];

    [self initTableView]; // initiate the tableview and works properly    
}

#pragma mark Camera Button 
- (void)addOverlayButton {
    self.oButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.oButton.frame = CGRectMake(132.0, self.originalOrigin, 56.0, 56.0);
    [self.oButton setImage:[UIImage imageNamed:@"CameraButton56x56.png"] forState:UIControlStateNormal];
    [self.oButton addTarget:self action:@selector(toCameraView:) forControlEvents:UIControlEventTouchUpInside];

    [self.view insertSubview:self.oButton aboveSubview:self.view];
}

/** Remove this function and the button stayed where I expected it
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGRect tableBounds = self.tableView.bounds;

    // floating Button; 
    CGRect floatingButtonFrame = self.oButton.frame;
    floatingButtonFrame.origin.y = self.originalOrigin + tableBounds.origin.y;
    self.oButton.frame = floatingButtonFrame;

    [self.view bringSubviewToFront:self.oButton]; // float over the tableHeader
}
*/
4

0 回答 0