0

Exactly what it says on the tin. I'm updating an app to support iOS 8+. AFAIK, this works fine on iOS 7. Works as well in the simulator (iOS 7 or 8).

Here's the code for one of the buttons in the viewDidLoad:

memberButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
memberButton.frame = CGRectMake(20, screenHeight - 50, 133, 24);
[memberButton setBackgroundColor:[UIColor colorWithRed:70.0/255.0 green:70.0/255.0 blue:70.0/255.0 alpha:1]];
[memberButton setBackgroundImage:[UIImage imageNamed:@"Member.png"] forState:UIControlStateNormal];
[memberButton setTintColor:[UIColor whiteColor]];
[memberButton addTarget:self action:@selector(memberButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:memberButton];

memberButton.hidden = NO;
memberButton.enabled = YES;

Upon running the app on the phone, the button is nowhere to be found. Obviously there's a lot more on that app than a single button; there's an image you can scroll through. Could that be responsible?

Here's the code for the advertisement style scrolling image:

pgCtr = [[UIPageControl alloc] initWithFrame:CGRectMake(160, screenHeight - 70, 0, 0)];
[pgCtr setTag:0];
pgCtr.numberOfPages=3;
pgCtr.autoresizingMask=UIViewAutoresizingNone;
[self.view addSubview:pgCtr];

for (int i=1; i<=3; i++) {
    pgCtr.currentPage = i;
    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"intro_%d.jpg",i]];
    UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake((i-1)*scr.frame.size.width, 0, scr.frame.size.width, scr.frame.size.height)];
    imgV.contentMode=UIViewContentModeScaleToFill;
    [imgV setImage:image];
    imgV.tag=i+1;
    [scr addSubview:imgV];
}

[scr setContentSize:CGSizeMake(scr.frame.size.width*3, scr.frame.size.height)];
pgCtr.currentPage = 0;
pgCtr.currentPageIndicatorTintColor = [UIColor whiteColor];
pgCtr.pageIndicatorTintColor = [UIColor grayColor];

Things I tried (latest in bold):

  • Setting CGRectMake(20, screenHeight - 50, 133, 24) to static values.
  • Putting the scrollview code above the button codes.
  • Commenting out the entire scrollview.
  • Output member.frame.origin.x and .y. In simulator, both return values. In the phone, both return 0.00000.

None has displayed the buttons.

Cœur
  • 37,241
  • 25
  • 195
  • 267
zack_falcon
  • 4,186
  • 20
  • 62
  • 108

6 Answers6

2

Please perform below steps* to know what/where's the actual problem with iOS8 and device or in your code.

  1. Instead of checking it with dynamic frame, just use static frame to show the button. For e.g. CGRectMake(0,0,300,100).

  2. If step-1 would show the button then, problem will surely in your frame calculation.

  3. If step-1 don't work then, add a border to all the other controls in that view, and check whether it get above on the button or not?

* @Zack, only you can find the answer of your question as we could give you few suggestions and not the solution, as we don't have access to your project. The best way is to keep doing trial and error for each scenario.

Hemang
  • 26,840
  • 19
  • 119
  • 186
  • Thanks for the suggestions. I tried 1, but the buttons still doesn't appear. I'm not sure what you mean with frame calculation in number 2. And I'm not sure how to do number 3. Apologies, I'm sorta new at this. – zack_falcon Feb 02 '15 at 13:17
  • @zack_falcon, by *frame calculation* I mean, you've to properly assign (x,y) and (width,height) for that button. Also, for 3rd step, you can set border by accessing `layer` property. Like this, `button.layer.borderWidth=1;` and `button.layer.borderColor = [UIColor redColor].CGColor;`. By doing so, you'll able to see the total area equipped by particular control on your view. I hope I'm clear to you. – Hemang Feb 02 '15 at 13:33
  • I see. Thanks for clearing it up. But if it didn't show up with static values, wouldn't that mean both the frame and layers are moot? For my latest test, I discarded the scrollview entirely - the buttons, strangely, still did not show up. – zack_falcon Feb 02 '15 at 13:37
  • Have you log subviews for your view? Check whether your button is added in subview or not? – Hemang Feb 03 '15 at 04:55
  • I decided to output member.frame.origin.x and .y. In simulator, both return values. In the phone, both return 0.00000. I'm not sure what this means. – zack_falcon Feb 03 '15 at 06:43
  • @zack_falcon, Cross check, if you've multiple builds installed for the same app? Even if not, then also remove that build and retry installing and then log. – Hemang Feb 04 '15 at 04:51
  • I'll try logging the subviews. I assume it's something like this? Do I just paste that code somewhere? Do I need to call it out first, or something? http://stackoverflow.com/questions/7243888/how-to-list-out-all-the-subviews-in-a-uiviewcontroller-in-ios – zack_falcon Feb 04 '15 at 15:10
1

I think u miss the

 [self.view addSubview: memberButton];
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
1

I recommend not to set view frames at -viewDidLoad. That method is called only once in UIViewController life cycle. Good practice is to initialize subviews at -viewDidLoad with CGRectZero as frame and update frames at -viewWillLayoutSubviews method.

It will look like this:

- (void)viewDidLoad {
   [super viewDidLoad];
   //
   pgCtr = [[UIPageControl alloc] initWithFrame:CGRectZero];
   /* setting properties */
}

- (void)viewWillLayoutSubviews {
   CGRect screenRect = [[UIScreen mainScreen] bounds];
   CGFloat screenWidth = screenRect.size.width;
   CGFloat screenHeight = screenRect.size.height;
   //
   memberButton.frame = CGRectMake(20, screenHeight - 50, 133, 24);
   //
   pgCtr.frame = CGRectMake(160, screenHeight - 70, 0, 0);
   // same for contentSize
}

Try to move frame setting at -viewWillLayoutSubviews and check if the problem still exist. Maybe that little hint will help.

Regards, Artem

Artem Loginov
  • 801
  • 7
  • 8
  • I'll try this, but I don't see a `viewWillLayoutSubviews`. There's a `viewDidLoad` and `didReceiveMemoryWarning`, and functions for buttons. Can I just add that in, or is there something else I need to do. – zack_falcon Feb 02 '15 at 13:27
  • 1
    Just start typing -viewWill.. and you will receive a suggestion on UIViewController life cycle methods. – Artem Loginov Feb 03 '15 at 10:13
  • I did as you suggested, but unfortunately, it didn't work. I decided to output member.frame.origin.x and .y. In simulator, both return values. In the phone, both return 0.00000. I'm not sure what this means. – zack_falcon Feb 03 '15 at 15:43
0

check if the scroll view is placed over the top of the button.Change the position of the botton and see if its still there.

good4pc
  • 711
  • 4
  • 17
0

The navigation bar might be messing with your button. Just to try it, try setting the frame to memberButton.frame = CGRectMake(20, screenHeight - 50 -64, 133, 24);

It has happened to me before.

Neva
  • 1,330
  • 9
  • 11
0

I bit the bullet and decided to add the buttons on the storyboard, and re-wrote some code around. And to my surprise, the buttons finally appeared.

As to why didn't I do this before, well, there are a lot of buttons. And a lot of view controllers for the buttons. And all of them are programmatically added. Given the size of the project, there's no way I can do them all on my own, but I posit it would be better to show something than nothing.

So this is more of band-aid, than an actual solution. For now, it'll do. If anyone else finally figures it out so the code doesn't go to waste, don't hesitate to answer.

zack_falcon
  • 4,186
  • 20
  • 62
  • 108