Martin
Martin

Reputation: 321

UIButton will not center in UITableViewCell when manually centered

I am working on a small exercise for myself and I am running into a small issue that perhaps someone can offer some assitance on. I have the TableViewCell setup to expand when tapping on the cell. This works great. My issue, is that the UIButton that I have within the TableViewCell will not center.

Note: I am not using Auto Layout in this application.

I have a ViewController which currently handles my main view. I have a XIB that I created and I have designed my TableViewCell for this exercise in.

custom xib

Looking at the layout of the XIB, everything looks like it should however when I run the app in the simulator, you can see that the UIButton shifts to the left slightly.

simulator

I have the Auto Resizing attributes setup as such, as if I do not, the UIButton is pushed into the TableViewCell which is not what I want.

auto resizing

Any idea why this does not center correctly?

Upvotes: 1

Views: 584

Answers (3)

Martin
Martin

Reputation: 321

Reading other posts, it seems that Arda's response should have worked but it was not working for me.

What I did was, enable Auto Layout on the XIB as well as Size Classes. I then realized that my XIB was not wide enough as it was not using Size Classes before which seemed to cause things to shift a bit within the TableViewCell. A few extra tweaks here in there and in my code and things are good.

Now when I run the app, things look correct.

Upvotes: 0

Yuri Chukhlib
Yuri Chukhlib

Reputation: 120

Sometimes it may not work( I had the same problem). Just do it programmatically and forget about this.

For example:

    UIButton * btn = [UIButton alloc] init];
    btn.frame.origin = CGPointMake(
    self.view.frame.width/2       - btn.frame.size.width/2,
    self.view.frame.size.height/2 - btn.frame.size.height/2);

Upvotes: 0

Arda Keskiner
Arda Keskiner

Reputation: 792

You can select your button and horizontally center it.

enter image description here

Upvotes: 1

Related Questions