我把DiscoverController放在一个NavigationController里面然后再添加到tabbarcontroller里 然后我定义了一个scrollview 它的frame是 (0 0 self.view.frame.size.width, 108) (这里设置108是因为20是状态栏+44导航栏+44是我希望可以显示的scrollview的高度) 然后我添加一个UIView到scrollview上 这个uiview的frame是(100,0,500, 44). 我希望达到的效果是这个uiview只能左右滚动不能上下滚动所以我还设置了scrollview的contentsize是(900, 0). 但是很奇怪我添加了这个uiview之后还是能上下滚动 可是当我把scrollview的Y值设置为-5或者scrollview的height设置为49, 就变得只能左右滚动不能上下滚动 我想知道这个5是来自哪里的 为什么要把Y设置成-5才能达到只能左右不能上下滚动? 一般设置scrollview的滚动不是用contentsize 来决定的吗??
问题可能我没有说的很清楚 反正就是有5个pixel我不知道是从哪里来的 想求各位大神指教
代码如下:
#import "DiscoverController.h"
#import "DiscoverNaviButton.h"
#import "LocationController.h"
@interface DiscoverController () <UITableViewDataSource, UITableViewDelegate, LocationControllerDelegate>
@property(nonatomic, strong) LocationController *locationList;
@property(nonatomic, strong) DiscoverNaviButton *btn;
@property(nonatomic, weak) UIScrollView *typeScrollView;
@end
@implementation DiscoverController
- (LocationController *)locationList{
if(!_locationList){
_locationList = [[LocationController alloc] initWithStyle:UITableViewStylePlain];
UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[rightBtn setImage:[UIImage imageNamed:@"rightBtn"] forState:UIControlStateNormal];
rightBtn.bounds = CGRectMake(0, 0, 20, 20);
[rightBtn addTarget:self action:@selector(dismissVC) forControlEvents:UIControlEventTouchUpInside];
_locationList.delegate = self;
UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
_locationList.navigationItem.rightBarButtonItem = rightBarBtn;
_locationList.location = @"Toronto";
}
return _locationList;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
[self setupScrollViewBtn];
[self setupButtonTitle];
}
- (void)setupScrollViewBtn{
CGFloat scrollViewHeight = self.navigationController.navigationBar.frame.origin.y + self.navigationController.navigationBar.frame.size.height + 49;
UIScrollView *typeScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, -5, self.view.frame.size.width, scrollViewHeight)];
typeScrollView.showsHorizontalScrollIndicator = NO;
typeScrollView.backgroundColor = [UIColor blueColor];
typeScrollView.contentSize = CGSizeMake(600, 0);
UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(100, 5, 400, 44)];
testView.backgroundColor = [UIColor yellowColor];
[typeScrollView addSubview:testView];
[self.view addSubview:typeScrollView];
}
- (void)setupButtonTitle{
DiscoverNaviButton *button = [[DiscoverNaviButton alloc] init];
button.frame = CGRectMake(0, 0, 80, 20);
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitle:self.locationList.location forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"naviBtnArrow"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(presentVC) forControlEvents:UIControlEventTouchUpInside];
self.btn = button;
self.navigationItem.titleView = self.btn;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)presentVC{
UINavigationController *naviLocation = [[UINavigationController alloc] initWithRootViewController:self.locationList];
[self presentViewController:naviLocation animated:YES completion:nil];
}
- (void)dismissVC{
[self.locationList dismissViewControllerAnimated:YES completion:nil];
self.locationList = nil;
}
- (void)locationController:(LocationController *)locationController didSelectLocation:(NSString *)location{
[self setupButtonTitle];
[self.locationList dismissViewControllerAnimated:YES completion:nil];
self.locationList = nil;
}
- (void)changeType:(UIButton *)btn{
btn.selected = !btn.isSelected;
}
@end
问题可能我没有说的很清楚 反正就是有5个pixel我不知道是从哪里来的 想求各位大神指教
代码如下:
#import "DiscoverController.h"
#import "DiscoverNaviButton.h"
#import "LocationController.h"
@interface DiscoverController () <UITableViewDataSource, UITableViewDelegate, LocationControllerDelegate>
@property(nonatomic, strong) LocationController *locationList;
@property(nonatomic, strong) DiscoverNaviButton *btn;
@property(nonatomic, weak) UIScrollView *typeScrollView;
@end
@implementation DiscoverController
- (LocationController *)locationList{
if(!_locationList){
_locationList = [[LocationController alloc] initWithStyle:UITableViewStylePlain];
UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[rightBtn setImage:[UIImage imageNamed:@"rightBtn"] forState:UIControlStateNormal];
rightBtn.bounds = CGRectMake(0, 0, 20, 20);
[rightBtn addTarget:self action:@selector(dismissVC) forControlEvents:UIControlEventTouchUpInside];
_locationList.delegate = self;
UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
_locationList.navigationItem.rightBarButtonItem = rightBarBtn;
_locationList.location = @"Toronto";
}
return _locationList;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
[self setupScrollViewBtn];
[self setupButtonTitle];
}
- (void)setupScrollViewBtn{
CGFloat scrollViewHeight = self.navigationController.navigationBar.frame.origin.y + self.navigationController.navigationBar.frame.size.height + 49;
UIScrollView *typeScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, -5, self.view.frame.size.width, scrollViewHeight)];
typeScrollView.showsHorizontalScrollIndicator = NO;
typeScrollView.backgroundColor = [UIColor blueColor];
typeScrollView.contentSize = CGSizeMake(600, 0);
UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(100, 5, 400, 44)];
testView.backgroundColor = [UIColor yellowColor];
[typeScrollView addSubview:testView];
[self.view addSubview:typeScrollView];
}
- (void)setupButtonTitle{
DiscoverNaviButton *button = [[DiscoverNaviButton alloc] init];
button.frame = CGRectMake(0, 0, 80, 20);
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitle:self.locationList.location forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"naviBtnArrow"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(presentVC) forControlEvents:UIControlEventTouchUpInside];
self.btn = button;
self.navigationItem.titleView = self.btn;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)presentVC{
UINavigationController *naviLocation = [[UINavigationController alloc] initWithRootViewController:self.locationList];
[self presentViewController:naviLocation animated:YES completion:nil];
}
- (void)dismissVC{
[self.locationList dismissViewControllerAnimated:YES completion:nil];
self.locationList = nil;
}
- (void)locationController:(LocationController *)locationController didSelectLocation:(NSString *)location{
[self setupButtonTitle];
[self.locationList dismissViewControllerAnimated:YES completion:nil];
self.locationList = nil;
}
- (void)changeType:(UIButton *)btn{
btn.selected = !btn.isSelected;
}
@end