开发者

how to get uibutton exactly same width and height of custom uitableviewcell

开发者 https://www.devze.com 2023-03-10 01:36 出处:网络
I am trying to get my button to be exactly the same width and height as my custom uitableviewcell. Here is how I\'m trying to do it

I am trying to get my button to be exactly the same width and height as my custom uitableviewcell.

Here is how I'm trying to do it

// Customize the appearance of table vie开发者_运维百科w cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            return cellCode;            
        }
        if (indexPath.row == 1) {
            cellManufacture.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            return cellManufacture;
        }
        if (indexPath.row == 2) {
            cellModel.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            return cellModel;
        }
        if (indexPath.row == 3) {
            cellYear.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            return cellYear;
        }
    }
    submitButton.frame = cellSubmit.bounds; //<<<<<<HERE IS WHERE I SET SIZING :)
    return cellSubmit;  
}

however this is the result I get....

how to get uibutton exactly same width and height of custom uitableviewcell


How about using Table Footer View with UIButton?

CGSize buttonSize = CGSizeMake(300, 45);
CGRect viewRect = CGRectMake(0, 0, self.view.frame.size.width, buttonSize.height);
CGRect buttonRect = CGRectMake((viewRect.size.width-buttonSize.width) / 2 , 
                               (viewRect.size.height-buttonSize.height) / 2 , 
                               buttonSize.width, 
                               buttonSize.height);

UIView *footerView = [[UIView alloc] initWithFrame: viewRect];
footerView.backgroundColor = [UIColor clearColor];

UIButton *submitButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
submitButton.frame = buttonRect;
[submitButton setTitle:@"SUBMIT" forState:UIControlStateNormal];
[footerView addSubview: submitButton];

[(UITableView *)self.view setTableFooterView: footerView];

[footerView release];

However, if you want exactly same appearance, you should use a default UITableCell(UITableViewCellStyleDefault) in the next section, not UIButton.

UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
cell.textLabel.text = @"SUBMIT";
cell.textLabel.textAlignment = UITextAlignmentCenter;
0

精彩评论

暂无评论...
验证码 换一张
取 消