这是我正在做的事情。我在UITableView的一行中有一个UITableViewCell
。
我现在已经创建了一个UIView并将其作为子视图添加到UITableViewCell.With在UIView中我创建了一个自定义按钮并且想要位于UIView的中心但是当我居中它时按钮从UIView中退出。
我为所附图片提供了背景颜色,以便更好地理解。
格雷:自定义UIButton 红色:UIView 绿色:UITableViewCell 蓝色:绿色的Lable
中心按钮后:
以下是居中后的相同代码:
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;
调用添加我的按钮:
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;
按钮居中的代码:
btnBinarySwitchOn.center = view.center;
答案 0 :(得分:2)
我认为问题在于这一行btnBinarySwitchOn.center = view.center;
也许这就是你想要的:
btnBinarySwitchOn.center = CGPointMake(view.frame.size.width/2,
view.frame.size.height/2);