对于UIScrollView *toScrollView(这是屏幕的宽度),我想添加一个灰色的底部边框(与iPhone原生消息应用程序的 compose 视图的To字段完全相同).

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.

这看起来不错,但这是一种黑客行为,我想知道是否有更好的方法来做到这一点.

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

推荐答案

正如@ImreKelényi所建议的,你可以用CALayer来代替UIView:

// 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];

Ios相关问答推荐

自定义alert 未执行任何操作

使用CGAffineTransform旋转视图只起作用一次吗?

RFID scanner 的蓝牙服务 UUID?

如何链式设置 AttributeContainer 的 UIKit 属性?

在自定义 ButtonStyle 中修改形状 colored颜色

表视图(SwiftUI)中的多列?

如何更改 SwiftUI 弹出视图的大小?

SKScene 的更新功能似乎不起作用

RealityKit – ARView 光线投射返回 0 个结果

SwiftUI 图表 BarMark 将注释对齐到图表顶部

clipShape swift的三元

UIViewControllerRepresentable 在 Xcode 14 iOS 16 上崩溃

Apple Push Service 证书不受信任

如何忽略touch 事件并将它们传递给另一个子视图的 UIControl 对象?

iOS WKWebView 不显示 javascript alert() 对话框

单击 GoogleSignIn 按钮时应用程序崩溃

使用 Swift 在一个 ViewController 中强制横向模式

UIView - 加载视图时如何获得通知?

SwiftUI 应用生命周期 iOS14 AppDelegate 代码放哪里?

Xcode 5 和 iOS 7:架构和有效架构