0

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: enter image description here

After center button:

enter image description here

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;  
Masood
  • 153
  • 8
  • I think it is because of the frame you set for the btnBinarySwitchOn. – PK20 Oct 26 '15 at 06:05
  • @PK20 commented it out and checked. Still the same – Masood Oct 26 '15 at 06:11
  • If that is not the reason, then check for your UIView frame. – PK20 Oct 26 '15 at 06:13
  • This [link](http://stackoverflow.com/questions/5361369/uiview-frame-bounds-and-center?rq=1) has more info on UIView frame, bunds and center – PK20 Oct 26 '15 at 06:18
  • @PK20 I changed the xy co-ordinates of UIView to (0,0) and the button is alinging well in center, but I want something like (0, 25), and this makes button to go out of UIView – Masood Oct 26 '15 at 06:26
  • try like `child.center = [parent convertPoint:parent.center fromView:parent.superview];` – PK20 Oct 26 '15 at 06:33
  • or like `child.center = CGPointMake(parent.bounds.height/2, parent.bounds.width/2)` either one should help – PK20 Oct 26 '15 at 06:34
  • Also see if it allows you to code like `btnBinarySwitchOn.center = view.view.center;` These are few options that you can try out – PK20 Oct 26 '15 at 06:37

1 Answers1

2

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);
tuledev
  • 10,177
  • 4
  • 29
  • 49
  • You should use the `bounds` and `not` the `frame`, as the frame is undefined if the view has a `transform`. – PK20 Oct 26 '15 at 06:37
  • @anhtu: thanks! it worked, can you explain what could have happened. – Masood Oct 26 '15 at 07:57
  • @PK20: sure, I will have a look at bounds. Yes, when I rotated the screen, the view did not stretch, neither with bounds or frame – Masood Oct 26 '15 at 07:57
  • @Masood the value of `view.center` is the value of **view's center in view's superview**. It's not the **center of the view**. – tuledev Oct 26 '15 at 08:03
  • @Masood try to log `view.center` and `CGPointMake(view.frame.size.width/2, view.frame.size.height/2)` to see what happen. :) – tuledev Oct 26 '15 at 08:14