[英]How to set corner radius of a view inside a view in tableview cell?
[英]How to set corner radius for TableView cells in ios
嗨,我是iOS的新手,在我的應用程序中,我正在使用TableList,並且我想將tableList單元角的半徑設置為3.0。 這是我的代碼。 它僅在左角而不是所有角上應用角半徑。
- (CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 75;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = @"MyCell";
Cell = (MyTripsCell *)[MaintableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (Cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyTripsCell" owner:self options:nil];
Cell = [nib objectAtIndex:0];
}
Cell.layer.cornerRadius = 0.0;
Cell.selectionStyle = UITableViewCellSelectionStyleNone;
return Cell;
}
-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *) cell forRowAtIndexPath:(NSIndexPath *)indexPath{
cell.contentView.backgroundColor = [Bg colorWithHexString:@"EEEEEE"];
UIView * whiteRoundedView = [[UIView alloc]initWithFrame:CGRectMake(0, 12, MaintableView.frame.size.width, 70)];
whiteRoundedView.layer.backgroundColor = [Bg colorWithHexString:@"FDFDFD"].CGColor;
whiteRoundedView.layer.masksToBounds = YES;
whiteRoundedView.layer.cornerRadius = 3.0;
whiteRoundedView.layer.shadowOffset = CGSizeMake(-1, -1);
[cell.contentView addSubview:whiteRoundedView];
[cell.contentView sendSubviewToBack:whiteRoundedView];
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
[cell setPreservesSuperviewLayoutMargins:NO];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
該代碼是可行的,因為它在每個拐角處都執行拐角半徑,但是問題是您實際上是在將視圖的原點移動到y = 12 。 視圖的寬度與tableView的寬度相同,並且它的拐角半徑是您在視圖超出屏幕右側時看不到的。
為了使視圖位於屏幕中間,請將其中心設置為contentView的中心。 並與寬度一起設置填充。
您的代碼解決方案:
// Using this width and setting the view to the center will give you padding of 12 pixels on left and right
NSInteger width = MaintableView.frame.size.width - 24;
UIView * whiteRoundedView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, width, 70)];
whiteRoundedView.center = cell.contentView.center;
whiteRoundedView.layer.backgroundColor = [Bg colorWithHexString:@"FDFDFD"].CGColor;
whiteRoundedView.layer.masksToBounds = YES;
whiteRoundedView.layer.cornerRadius = 3.0;
whiteRoundedView.layer.shadowOffset = CGSizeMake(-1, -1);
[cell.contentView addSubview:whiteRoundedView];
[cell.contentView sendSubviewToBack:whiteRoundedView];
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
[cell setPreservesSuperviewLayoutMargins:NO];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
無論如何,我可以通過在每次單元出隊時不添加視圖來對代碼進行一些改進。 還要注意,為此視圖設置maskToBounds = YES
會導致您的陰影不顯示(請參閱此處 )
UIView *roundedView = [cell.contentView viewWithTag:100];
if (roundedView == nil) {
// Add some padding to the view in order to see the shadow
NSInteger spacingBothHorizontal = 2;
CGRect customizedFrame = CGRectMake(spacingBothHorizontal/2, 0, CGRectGetWidth(cell.contentView.frame) - spacingBothHorizontal, CGRectGetHeight(cell.contentView.frame) - 20);
roundedView = [[UIView alloc] initWithFrame:customizedFrame];
// Layer customizations
roundedView.layer.cornerRadius = 10.0f;
roundedView.backgroundColor = [UIColor redColor];
roundedView.layer.shadowOffset = CGSizeMake(-1, -1);
roundedView.layer.shadowOpacity = 2.0;
roundedView.layer.shadowColor = [UIColor blueColor].CGColor;
roundedView.tag = 100;
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
[cell setPreservesSuperviewLayoutMargins:NO];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
// Add it to view
[cell.contentView addSubview:roundedView];
[cell.contentView sendSubviewToBack:roundedView];
cell.contentView.backgroundColor = [UIColor blackColor];
}
可能的原因可能是:
您的單元格高度為75。您正在編寫以下代碼:
UIView * whiteRoundedView = [[UIView alloc]initWithFrame:CGRectMake(0, 12, MaintableView.frame.size.width, 70)];
查看實際的CGRectMake
CGRect CGRectMake ( CGFloat x, CGFloat y, CGFloat width, CGFloat height );
在您的情況下y =12。當您放置一個子視圖(您的whiteRoundedView
)為( whiteRoundedView
(0,0,width,height)
時(0,0,width,height)
它是根據單元格坐標的whiteRoundedView
視圖(0,0,width,height)
,它的下部將不顯示。 由於其出了單元格視口。 嘗試了解這些坐標系。
編輯:
將您的查看代碼更改為此:
UIView * whiteRoundedView = [[UIView alloc]initWithFrame:CGRectMake(0, 3, cell.frame.size.width, 69)];
並將角半徑設置為10。
whiteRoundedView.layer.cornerRadius = 10.0;
運行該應用程序,看看有什么不同。
這是示例代碼。 這是您要找的嗎?
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
}
if ([cell viewWithTag:100] == nil)
{
UIView* roundedView = [[UIView alloc]initWithFrame:CGRectInset(cell.bounds, 2, 2)];
roundedView.backgroundColor = [UIColor lightGrayColor];
roundedView.tag = 100;
roundedView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[cell insertSubview:roundedView atIndex:0];
roundedView.layer.cornerRadius = 5;
roundedView.layer.masksToBounds = true;
}
return cell;
}
在您的情況下,請使用以下命令並刪除willdisplaycell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if ([cell viewWithTag:100] == nil)
{
UIView* roundedView = [[UIView alloc]initWithFrame:CGRectInset(cell.bounds, 2, 2)];
roundedView.backgroundColor = [UIColor lightGrayColor];
roundedView.tag = 100;
roundedView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[cell insertSubview:roundedView atIndex:0];
roundedView.layer.cornerRadius = 5;
roundedView.layer.masksToBounds = true;
}
cell.textLabel.text = @"Hello world";
return cell;
}
- (CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 75;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.