3

I am doing

mapButton *topMapButton = [[mapButton alloc] init];
    [topMapButton setSize:CGSizeMake(70, 30)];
    topMapButton.frame = CGRectMake(0, STATUS_HEIGHT +7, 70, 30);

    UIBarButtonItem *barIcon = [[UIBarButtonItem alloc] initWithCustomView: topMapButton];
    [topMapButton addTarget:self action:@selector(openMap:) forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.rightBarButtonItem = barIcon;

where mapButton is a UIButton subclass, with preset look, nothing else

No matter what frame I set for topMapButton, it keeps being in (0,0) and not in the rightBarButtonItem usual place.

Note that using autolayout programatically to set the place works for placing, but crashes when I push another controller in the stack...

Am I doing anything wrong?

Nick Ginanto
  • 31,090
  • 47
  • 134
  • 244

2 Answers2

1

Look at this answer

Shortly, you can set edge inset. override alignmentRectInsets for your uibutton subclass and change top and left insets.

- (UIEdgeInsets)alignmentRectInsets {
    UIEdgeInsets insets =  UIEdgeInsetsMake(top_value, left_value, bottom_value, right_value);

    return insets;
}
Community
  • 1
  • 1
Doro
  • 2,413
  • 2
  • 14
  • 26
0

OK found the problem..

It seems that UIBarButtonItem doesn't really care what frame the customView has, since it will set it anyway.

however, my custom button MUST have a custom type, otherwise it will mess the frame and keep it (0,0) of the application screen.

self = [UIButton buttonWithType:UIButtonTypeCustom];
Nick Ginanto
  • 31,090
  • 47
  • 134
  • 244