Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UIC控件背景色_Iphone_Objective C_Ios_Uirefreshcontrol - Fatal编程技术网

Iphone UIC控件背景色

Iphone UIC控件背景色,iphone,objective-c,ios,uirefreshcontrol,Iphone,Objective C,Ios,Uirefreshcontrol,是否可以使UIRefreshControl的背景随着控件的增长而增长 我希望刷新控件有一个彩色背景,以匹配顶部单元格的背景色。更改tableview的背景色是不可接受的,因为这样底部的空单元格也会有颜色,但我需要它们保持白色 苹果的邮件应用程序显示了这种行为。刷新控件的背景与灰色搜索栏匹配,但表视图底部的空单元格仍然是正常的白色 下面是一个示例屏幕截图,显示了在拖动刷新控件时出现的难看的白色: 您必须创建一个带有bgColor的视图,并在tableView中添加带有负y原点的视图 警告: 必须

是否可以使UIRefreshControl的背景随着控件的增长而增长

我希望刷新控件有一个彩色背景,以匹配顶部单元格的背景色。更改tableview的背景色是不可接受的,因为这样底部的空单元格也会有颜色,但我需要它们保持白色

苹果的邮件应用程序显示了这种行为。刷新控件的背景与灰色搜索栏匹配,但表视图底部的空单元格仍然是正常的白色

下面是一个示例屏幕截图,显示了在拖动刷新控件时出现的难看的白色:

您必须创建一个带有bgColor的视图,并在tableView中添加带有负y原点的视图

警告:

  • 必须在tableView子视图的底部堆栈中插入此视图
  • 设置refreshControll后必须插入此视图:self.refreshControl=refreshControl
如果不以这种方式插入此视图,则不会看到刷新控件:它将隐藏在视图下方

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Background Color
    UIColor *bgRefreshColor = [UIColor grayColor];

    // Creating refresh control
    refreshControl = [[UIRefreshControl alloc] init];
    [refreshControl addTarget:self action:@selector(refresh) forControlEvents:UIControlEventValueChanged];
    [refreshControl setBackgroundColor:bgRefreshColor];
    self.refreshControl = refreshControl;

    // Creating view for extending background color
    CGRect frame = self.tableView.bounds;
    frame.origin.y = -frame.size.height;
    UIView* bgView = [[UIView alloc] initWithFrame:frame];
    bgView.backgroundColor = bgRefreshColor;
    bgView.autoresizingMask = UIViewAutoresizingFlexibleWidth;

    // Adding the view below the refresh control
    [self.tableView insertSubview:bgView atIndex:0]; // This has to be after self.refreshControl = refreshControl;
}

Swift版本,便于复制粘贴:)

Swift 2

func viewDidLoad() {
    super.viewDidLoad()

    // Background Color
    let backgroundColor = UIColor.grayColor()

    // Creating refresh control
    let refresh = UIRefreshControl()
    refresh!.backgroundColor = backgroundColor
    refresh!.addTarget(self, action: #selector(refresh), forControlEvents: UIControlEvents.ValueChanged)
    refreshControl = refresh

    // Creating view for extending background color
    var frame = tableView.bounds
    frame.origin.y = -frame.size.height
    let backgroundView = UIView(frame: frame)
    backgroundView.autoresizingMask = .FlexibleWidth
    backgroundView.backgroundColor = backgroundColor

    // Adding the view below the refresh control
    tableView.insertSubview(backgroundView, atIndex: 0) // This has to be after refreshControl = refresh
}
Swift 3

func viewDidLoad() {
    super.viewDidLoad()

    // Background Color
    let backgroundColor = .gray

    // Creating refresh control
    let refresh = UIRefreshControl()
    refresh!.backgroundColor = backgroundColor
    refresh!.addTarget(self, action: #selector(refresh), forControlEvents: .valueChanged)
    refreshControl = refresh

    // Creating view for extending background color
    var frame = tableView.bounds
    frame.origin.y = -frame.size.height
    let backgroundView = UIView(frame: frame)
    backgroundView.autoresizingMask = .flexibleWidth
    backgroundView.backgroundColor = backgroundColor

    // Adding the view below the refresh control
    tableView.insertSubview(backgroundView, atIndex: 0) // This has to be after refreshControl = refresh
}
现在,只需设置UIRefreshControl背景色属性即可实现(Swift 5,iOS 13):

let ctrl = UIRefreshControl(frame: .zero)    
ctrl.backgroundColor = UIColor.gray'
tableView.refreshControl = ctrl

你能解释一下你的警告吗?我想知道你为什么警告我们可以改进你的回答。完成!这是为了避免你的刷新控件被你添加的背景视图隐藏+1谢谢它只是增加了细节,并有助于向正在学习的新手解释。谢谢Alex!这正是我所需要的。如果您的视图支持旋转,那么您可能需要在
bgView
上添加一个
autoresizingMask
,以确保其适当拉伸<代码>bgView.autoresizingMask=UIViewAutoresizingFlexibleWidth