【问题标题】:How can i round corners of container table view?我怎样才能圆容器表视图的角落?
【发布时间】:2014-03-15 00:07:32
【问题描述】:

我有一个主视图控制器。它包含一个表格视图控制器(在一个容器中),我想对表格视图进行四舍五入,使其显示类似于 facebook 登录。

我在 viewDidLoad 的子视图控制器(它是一个 tableview 控制器)中使用的代码:

self.tableView.layer.cornerRadius = 10.0f;
self.tableView.layer.masksToBounds = YES;
self.tableView.clipsToBounds = YES;
self.tableView.backgroundColor = [UIColor clearColor];

结果:

如您所见,当该字段被选中时,角看起来是圆的,但空白仍然存在。即使没有选择,我怎样才能使它们四舍五入?

使用:XCODE 5,IOS 7

【问题讨论】:

  • 您是否尝试将其应用于超级层?
  • 如果你的意思是 self.tableview.superview.layer.cornerradius = x ,不行,试一下。
  • view.layer.superlayer 呢?
  • 您是否尝试过 for 循环来设置所有子层的角半径?可能会这样做
  • 你能把代码写成答案还是在这里让我试试?我假设我会在父视图控制器中使用循环。

标签: ios xcode5 uitableview cornerradius uicontainerview


【解决方案1】:

按照你的要求,这里是你想要的代码:

for (CALayer *subLayer in self.tableView.layer.sublayers)
{
    subLayer.cornerRadius = 10;
    subLayer.masksToBounds = YES;
}

【讨论】:

  • 这非常有效。我很感激那里有比我更聪明的人,谢谢!
【解决方案2】:

试试这个

@interface CustomtableViewController : UITableViewController<UITableViewDelegate, UITableViewDataSource>
{
    UITextField * username;
    UIButton * submit;
}

@implementation CustomtableViewController

- (void)viewDidLoad
{
    UIView *newView = [[UIView alloc]initWithFrame:CGRectMake(10, 70, 300, 45)];
    submit = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [submit setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    //[submit setTitleColor:[UIColor colorWithWhite:0.0 alpha:0.56] forState:UIControlStateDisabled];
    [submit setTitle:@"Login" forState:UIControlStateNormal];
    [submit.titleLabel setFont:[UIFont boldSystemFontOfSize:14]];
    [submit setFrame:CGRectMake(10.0, 15.0, 280.0, 44.0)];
    [newView addSubview:submit];

    [self.tableView setTableFooterView:newView];

   [super viewDidLoad];

}

  #pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        //self.tableView.contentOffset = CGPointMake( 10,  320);
        [self.tableView setContentInset:UIEdgeInsetsMake(50,0,0,0)];
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    if ([indexPath section] == 0) {
        username = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
        username.adjustsFontSizeToFitWidth = YES;
        username.textColor = [UIColor blackColor];
        if ([indexPath row] == 0) {
            username.placeholder = @"example@gmail.com";
            username.keyboardType = UIKeyboardTypeEmailAddress;
            username.returnKeyType = UIReturnKeyNext;
            cell.textLabel.text = @"Username";
            username.clearButtonMode = YES;
        }
        else {
            username.placeholder = @"minimum 6 characters";
            username.keyboardType = UIKeyboardTypeDefault;
            username.returnKeyType = UIReturnKeyDone;
            username.secureTextEntry = YES;
            cell.textLabel.text = @"Password";
            username.clearButtonMode = UITextFieldViewModeAlways;
        }
        username.backgroundColor = [UIColor whiteColor];
        username.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
        username.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support
        username.textAlignment = NSTextAlignmentLeft;
        username.tag = 0;


        username.clearButtonMode = UITextFieldViewModeAlways; // no clear 'x' button to the right
        [username setEnabled: YES];


    [cell.contentView addSubview: username];

         }

    // Configure the cell...

    return cell;
}

在这里,我只为用户名和密码创建了两个文本字段。您可以根据需要使用 else if 条件在每个连续行中插入任何文本字段。

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [NSString stringWithFormat:@"User Login"];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {

    return 50;
}


- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {

    return @"";

}

所以,我这里的代码仅用于创建一个包含两个文本字段(用户名和密码)和一个登录按钮的登录页面。您可以根据需要修改我的代码。干杯!

【讨论】:

  • 我不想重写整个控制器,角落变得圆润但没有被剪裁。必须有一个更简单的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-24
  • 2023-01-01
  • 1970-01-01
相关资源
最近更新 更多