Reputation: 153
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
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;
Upvotes: 0
Views: 836
Reputation: 10327
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);
Upvotes: 2