UIView底部边界? [英] UIView bottom border?

查看:116
本文介绍了UIView底部边界?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UIScrollView * toScrollView (这是屏幕的宽度),我想添加一个灰色的底部边框(完全类似于to-field的撰写iPhone原生消息应用程序的视图。

To a UIScrollView *toScrollView (which is the width of the screen), I want to add a gray bottom border (exactly like that of the to-field of the compose view of the iPhone's native Messages app).

为实现这一目标,我遵循了 Cocoa Touch:如何改变UIView的边框颜色和厚度?并且只是用自定义<$覆盖顶部边框c $ c> UINavigationBar 并使 toScrollView 的x坐标-1&宽度322使左和右;正确的边框就在屏幕外。

To achieve this, I followed Cocoa Touch: How To Change UIView's Border Color And Thickness? and just covered the top border with the custom UINavigationBar and made the toScrollView's x-coordinate -1 & width 322 so that the left & right borders are just off screen.

这看起来很好,但它有点像黑客,我想知道是否有更好的方法来做到这一点。

This looks fine, but it's sort of a hack, and I was wondering if there's a better way to do this.

- (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
}


推荐答案

而不是使用 UIView ,正如@ImreKelényi建议的那样,您可以使用 CALayer

Instead of using a UIView, as @ImreKelényi suggests, you can use a CALayer:

// Add a bottomBorder.
CALayer *bottomBorder = [CALayer layer];

bottomBorder.frame = CGRectMake(0.0f, 43.0f, toScrollView.frame.size.width, 1.0f);

bottomBorder.backgroundColor = [UIColor colorWithWhite:0.8f 
                                                 alpha:1.0f].CGColor;

[toScrollView.layer addSublayer:bottomBorder];

这篇关于UIView底部边界?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆