简体   繁体   中英

Unable to center a custom uibutton within a uiview dynamically

Here is what I am doing. I have a UITableViewCell within a row of UITableView.

I have now created a UIView and added it as subview to UITableViewCell.Within the UIView I have created a custom button and wanted to be in the center of the UIView but when I center it the button goes down out of the UIView.

I gave background colors for the images attached for better understanding.

Grey: custom UIButton Red: UIView Green: UITableViewCell Blue: Lable which is over Green

Before centering button: 在此处输入图片说明

After center button:

在此处输入图片说明

Here is the code for the same after centering:

tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath: 

NSString *cellidentifier=@"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellidentifier];
if(cell==nil){
    cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}

cell.backgroundColor = [UIColor greenColor];

//adding deviceName label
SFIDevice *device=(SFIDevice *)[self.deviceArray objectAtIndex:indexPath.row];
UIFont *font = [UIFont fontWithName:@"Avenir-Heavy" size:14];
UILabel *deviceNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(cell.frame.origin.x, cell.frame.origin.y, self.view.frame.size.width, 25)];
deviceNameLabel.text = device.deviceName;
deviceNameLabel.textAlignment=UITextAlignmentCenter;
deviceNameLabel.font=font;
deviceNameLabel.backgroundColor = [UIColor blueColor];
[cell addSubview:deviceNameLabel];

SensorIndexSupport *Index=[[SensorIndexSupport alloc]init];
NSArray *deviceIndexes=[Index getIndexesFor:device.deviceType];

UIView *cellView = [self addMyButton:cell withYScale:25  withDeviceIndex:deviceIndexes];
cellView.backgroundColor = [UIColor redColor];

return cell;

calling add my button:

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(cellFrame.frame.origin.x,
                                                              yScale,
                                                              self.view.frame.size.width,
                                                              frameSize)];
[cellFrame addSubview:view];
int i=0;
for (SFIDeviceIndex *deviceIndex in deviceIndexes) {
    for (IndexValueSupport *iVal in deviceIndex.indexValues) {
        i++;

        SFIRulesSwitchButton *btnBinarySwitchOn = [[SFIRulesSwitchButton alloc] initWithFrame:CGRectMake(0,0, frameSize, frameSize)];
        SFIDimmerButton *dimbtn=[[SFIDimmerButton alloc]initWithFrame:CGRectMake(view.frame.origin.x,view.frame.origin.y, dimFrameWidth, dimFrameHeight)];
        btnBinarySwitchOn.backgroundColor = [UIColor blueColor];

        btnBinarySwitchOn.selected = NO;
        [btnBinarySwitchOn addTarget:btnBinarySwitchOn action:@selector(onButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [btnBinarySwitchOn setupValues:[UIImage imageNamed:iVal.iconName] Title:iVal.displayText];

        btnBinarySwitchOn.frame = CGRectMake(btnBinarySwitchOn.frame.origin.x + ((i-1) * (frameSize/2))+textHeight/2 ,
                                             btnBinarySwitchOn.frame.origin.y,
                                             btnBinarySwitchOn.frame.size.width,
                                             btnBinarySwitchOn.frame.size.height);

        btnBinarySwitchOn.center = view.center;
        [view addSubview:btnBinarySwitchOn];

    }

}
return view;

code for centering the button:

btnBinarySwitchOn.center = view.center;  

I think the problem is this line btnBinarySwitchOn.center = view.center;

Maybe this is what you want:

btnBinarySwitchOn.center = CGPointMake(view.frame.size.width/2,
                                       view.frame.size.height/2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM