我想在宽度与屏幕相同的UIScrollView *toScrollView
上添加一个灰色底部边框(就像iPhone本机“信息”应用程序中的“到”字段一样)。
为了实现这一点,我遵循了Cocoa Touch:如何更改UIView的边框颜色和厚度?的建议,并使用自定义的UINavigationBar
覆盖了顶部边框,并使toScrollView
的x坐标为-1和宽度为322,以便左右边框刚好不显示在屏幕外。
看起来还不错,但这有点像是个hack,我想知道是否有更好的方法来实现这个效果。
- (void)viewDidLoad {
[super viewDidLoad];
// Add UINavigationBar *navigationBar at top.
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self action:@selector(cancelAction)];
UINavigationBar *navigationBar = [[UINavigationBar alloc]
initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)];
navigationBar.items = [NSArray arrayWithObject:self.navigationItem];
// Add UIScrollView *toScrollView below navigationBar.
UIScrollView *toScrollView = [[UIScrollView alloc]
initWithFrame:CGRectMake(-1.0f, 43.0f, 322.0f, 45.0f)];
toScrollView.backgroundColor = [UIColor whiteColor];
toScrollView.layer.borderColor = [UIColor colorWithWhite:0.8f alpha:1.0f].CGColor;
toScrollView.layer.borderWidth = 1.0f;
[self.view addSubview:toScrollView];
[self.view addSubview:navigationBar]; // covers top of toScrollView
}