1

I want to Center a logo on the SideMenu and this is the code so far:

  UIImageView *logo =[[UIImageView alloc] init];
    logo.image=[UIImage imageNamed:@"menulogo"];
    logo.contentMode = UIViewContentModeScaleToFill;
    logo.layer.cornerRadius = cornerRadius;
    logo.layer.masksToBounds = YES;
    logo.frame = container.bounds;
    [container addSubview:logo];
    [headerView addSubview:container];
Neo
  • 149
  • 1
  • 10
  • 2
    You've set the logo's frame to be identical to the container's, so it *can't* be centered. – NRitH Oct 03 '18 at 21:46

2 Answers2

1

Create an extension to UIView

extension UIView {  
func constraintToMidCenterXY(of view: UIView) {
    translatesAutoresizingMaskIntoConstraints = false

    NSLayoutConstraint.activate([centerXAnchor.constraint(equalTo: view.centerXAnchor),
                                 centerYAnchor.constraint(equalTo: view.centerYAnchor))
}

}

Then you can do something like this

logo.constraintToMidCenterXY(of: container)

Also you should not set logo's frame to container's bounds! that's another problem

The above is a common approach that you can use, Autolayout. You can also use UIStackView

lionserdar
  • 2,022
  • 1
  • 18
  • 21
0

A good solution is to use Auto Layout to center your logo to the headerView. It will adapt accordingly to any iPhone screen size.

Miguel Tepale
  • 289
  • 5
  • 15