Я потратил как минимум час на изучение темы. Наконец, у меня появилась идея использовать прозрачную рамку:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "faqCell", for: indexPath)
// ...
cell.layer.borderWidth = CGFloat(TABLE_CELLSPACING)
cell.layer.borderColor = tableView.backgroundColor?.cgColor
return cell
}
Это отлично работает в Swift 3 / Xcode 8.
Вы не должны помещать код CG в initWithCoder . Это сообщение следует использовать только для целей ИНИЦИАЛИЗАЦИИ.
Поместите код чертежа в:
- (void)drawRect:(CGRect)rect
Если вы создаете подкласс UIView ...
Начиная с шаблона на основе представления, создайте проект с именем Drawer . Добавьте в свой проект класс UIView. Назовите его SquareView (.h и .m).
Дважды щелкните DrawerViewController.xib , чтобы открыть его в Interface Builder . Измените общий вид на SquareView в Инспекторе удостоверений (команда-4) с помощью всплывающего меню Класс . Сохраните и вернитесь к Xcode .
Поместите этот код в метод drawRect: вашего файла SquareView.m , чтобы нарисовать большой изогнутый пустой желтый прямоугольник и маленький, зеленый прозрачный квадрат:
- (void)drawRect:(CGRect)rect;
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 0.0, 1.0); // yellow line
CGContextBeginPath(context);
CGContextMoveToPoint(context, 50.0, 50.0); //start point
CGContextAddLineToPoint(context, 250.0, 100.0);
CGContextAddLineToPoint(context, 250.0, 350.0);
CGContextAddLineToPoint(context, 50.0, 350.0); // end path
CGContextClosePath(context); // close path
CGContextSetLineWidth(context, 8.0); // this is set from now on until you explicitly change it
CGContextStrokePath(context); // do actual stroking
CGContextSetRGBFillColor(context, 0.0, 1.0, 0.0, 0.5); // green color, half transparent
CGContextFillRect(context, CGRectMake(20.0, 250.0, 128.0, 128.0)); // a square at the bottom left-hand corner
}
Вам не нужно вызывать этот метод, чтобы рисование произошло.