Button not showing on phone but yes on XCode simulator - ios

I've created a popup UIView that is a subview of my main view and a UIButton that is a close button that is a subview of my main view.
As shown in the picture the button shows in the XCode iPhone simulator in my Mac but when I test it in the physical phone the ICON/Button is not showing.
Why is not showing on my actuall phone but yes on the simulator? I've no errors, not even in the Phone console.... I have no idea how to debug this.
- (void) addCloseButton:(UIView*)myView {
CGRect position = CGRectMake(myView.frame.origin.x - 20, myView.frame.origin.y -20, 40, 40);
UIButton* myCloseButton = [[UIButton alloc]initWithFrame:position];
[myCloseButton setBackgroundImage:[UIImage imageNamed:#"close"] forState:UIControlStateNormal];
myCloseButton.tag = 45;
[myCloseButton addTarget:self action:#selector(removePopup)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:myCloseButton];
}

Try tapping where the button should be. If that works as intended, it is probably just a problem loading the asset.
The simulator is case-insensitive, devices are not.

Related

iOS button frame incorrect on iPhone 6 simulator, works on iPhone 4s

In my view controller, my view is initialized in loadView method:
- (void)loadView
{
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self loadViewController];
[self.view setBackgroundColor:[UIColor whiteColor]];
}
The loadViewController method creates a button and adds it to the view. Here's a snippet from the loadViewController method that initializes and positions the button:
graphicalButton = [UIButton buttonWithType:UIButtonTypeCustom];
graphicalButton.exclusiveTouch = YES;
[graphicalButton setImage:kGraphicalIconSelected
forState:UIControlStateSelected];
[graphicalButton setImage:kGraphicalIconDeSelected
forState:UIControlStateNormal];
[graphicalButton setFrame:CGRectMake(230, 100, 37, 34)];
[graphicalButton addTarget:self
action:#selector(graphicalOptionAction:)
forControlEvents:UIControlEventTouchUpInside];
[graphicalButton setUserInteractionEnabled:NO];
NSLog(#"graphical button x %f",graphicalButton.frame.origin.x);
[self.view addSubview:graphicalButton];
This displays the button correctly on an iPhone 4s simulator. The log statement logs the origin correctly as well.
However, on an iPhone 6 simulator, the log statement always logs the origin.x as 0. The button does not get displayed if the code runs. If however, I set up a breakpoint at the beginning of this code snippet and then step through each line, it logs the origin.x correctly and the button is displayed even on an iPhone 6 simulator.
I have even tried adding the call to loadViewController in viewWillAppear method, but even there the same issue occurs with this button. Any ideas on why this happens and how to resolve this?

UIButton not work in Landscape mode

After rotation of iPad in my simulator i found a strange bug - my UIButton have visible title, it property enables is YES, but, its not work. I tried to set background colour of button to black, and found that after rotation it vanish. Please take a look at portrait mode (all work):
And landscape mode (not work):
This is how i create button (i doubt that it help though, because its pretty mundane):
self.readNextButton = [UIButton new];
self.readNextButton.backgroundColor = [UIColor blackColor];
// self.readNextButton.titleLabel.adjustsFontSizeToFitWidth = YES;
[self.readNextButton setTitleColor:[UIColor colorWithHexString:#"#60aabf"] forState:UIControlStateNormal];
self.readNextButton.titleLabel.lineBreakMode = NSLineBreakByClipping;
self.readNextButton.titleLabel.font = [UIFont fontWithName:#"Times New Roman" size:14];
[self.readNextButton setTitle:#"Читать дальше" forState:UIControlStateNormal];
[self.view addSubview:self.readNextButton];
Also, i want to notice that this bug appears even when i'm not rotating anything, but simply load app in landscape mode. And also, ENABLED property of button is 1 (mean YES), when i log it..
Button frame:
[self.readNextButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.containerForNewsText.mas_right).with.offset(-10.834);
make.bottom.equalTo(self.containerForNewsText.mas_bottom).with.offset(-10.834);
make.top.equalTo(self.truncatedNewsText.mas_bottom).with.offset(20);
}];
It is in a method 'createLandscapeConstraints', that called in end of viewDidLoad.

iOS 9 View Not appearing

I have noticed a that a uibutton is not showing in my app when run in iOS 9 (works fine in earlier OS versions). I know several have noticed a similar issue with storyboards (https://forums.developer.apple.com/thread/14003), though I am not using those and am instead building my view programmatically. There is no autolayout or autoresizingmask to worry about.
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(SUG_MARGIN, self.mainView.bounds.size.height - 40 - margin, self.mainView.bounds.size.width-margin*2, 40);
[button setTitle:NSLocalizedString(#"...", nil) forState:UIControlStateNormal];
button.titleLabel.font = //...;
button.backgroundColor = //...;
[self.mainView addSubview:button];
NSLog(#"main:%#\nadd:%#\nsuper:%#", NSStringFromCGRect(self.mainView.frame), NSStringFromCGRect(button.frame), button.superview);
That nslog shows everything set up correctly.
I have similar buttons all over the app, but this is the only one that is not appearing, which is why I am scratching my head.
Refactored code to assign only measure frames in viewWillLayoutSubviews and it now works. strange.

Why would this code work on iPhone 5, 4 and 4s but not 5s?

I have the below code for adding some UIBarButtons to my project. This works just fine on the following:
All on iOS 7.1
Simulator iPhone Retina 3.5 inch;
Simulator iPhone Retina 4 inch;
Simulator iPhone Retina 4 inch (64 bit)
Iphone 4
iPhone 4s
I don't have an iPhone 5 device to test though.
It doesn't work on a new iPhone 5s. Whats different?
Here is the code:
-(void)setupNavigationBar
{
self.saveSearchButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.saveSearchButton setImage:[UIImage imageNamed:#"Save Search"] forState:UIControlStateNormal];
[self.saveSearchButton setImage:[UIImage imageNamed:#"Save Search Active"] forState:UIControlStateHighlighted|UIControlStateSelected];
[self.saveSearchButton addTarget:self action:#selector(saveSearchButtonpressed)forControlEvents:UIControlEventTouchUpInside];
[self.saveSearchButton setFrame:CGRectMake(0, 0, 23, 31)];
self.changeLayutButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.changeLayutButton setImage:[UIImage imageNamed:#"View List"] forState:UIControlStateNormal];
[self.changeLayutButton setImage:[UIImage imageNamed:#"View Grid"] forState:UIControlStateHighlighted|UIControlStateSelected];
[self.changeLayutButton addTarget:self action:#selector(changeViewLayoutButtonPressed)forControlEvents:UIControlEventTouchUpInside];
[self.changeLayutButton setFrame:CGRectMake(0, 0, 23, 31)];
self.sortByButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.sortByButton setImage:[UIImage imageNamed:#"Sort By"] forState:UIControlStateNormal];
[self.sortByButton setImage:[UIImage imageNamed:#"Sort By Active"] forState:UIControlStateHighlighted|UIControlStateSelected];
[self.sortByButton addTarget:self action:#selector(sortByButtonPressed)forControlEvents:UIControlEventTouchUpInside];
[self.sortByButton setFrame:CGRectMake(0, 0, 23, 31)];
UIBarButtonItem *fixedItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
fixedItem.width = 20;
UIBarButtonItem *saveSearchButton = [[UIBarButtonItem alloc] initWithCustomView:self.saveSearchButton];
UIBarButtonItem *changeViewButton = [[UIBarButtonItem alloc] initWithCustomView:self.changeLayutButton];
UIBarButtonItem *sortByButton = [[UIBarButtonItem alloc] initWithCustomView:self.sortByButton];
NSArray *barbuttonitems = #[sortByButton, fixedItem, changeViewButton, fixedItem,saveSearchButton];
self.navigationItem.rightBarButtonItems = barbuttonitems;
self.lastLayoutUsed = [[NSUserDefaults standardUserDefaults]objectForKey:#"LastLayoutUsed"];
if ([self.lastLayoutUsed isEqualToString:#"GridLayout"]){
self.changeLayutButton.selected = YES;
[NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:#selector(highlightButton:) userInfo:self.changeLayutButton repeats:NO];
}
}
I've stepped through the code and all the properties are not nil and have valid values. I've checked all images are the correct side, too.
How would this work on every device on simulator and not an actual iPhone 5S?
Also, since the two phones (iPhone 4S and 5S) have the exact same screen width and iOS version I am really puzzled?
The buttons don't show up at all. No compiler warning and no console errors.
UPDATE
Tested the above code on a iPhone 5 and it works just fine. This leads be to believe it must be something to do with the 64bit of the iPhone 5S?
UPDATE 2
Removed all the code from the method and changed it to a very simple button like this:
self.saveSearchButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.saveSearchButton setTitle:#"Save" forState:UIControlStateNormal];
[self.saveSearchButton setTintColor:[UIColor blueColor]];
UIBarButtonItem *saveSearchButton = [[UIBarButtonItem alloc] initWithCustomView:self.saveSearchButton];
self.navigationItem.rightBarButtonItem = saveSearchButton;
This now doesn't work on any devices or simulators.
What am I doing wrong here?
Update 3 - Solution!
Right, so it was one simple thing that caused all this fuss. I declared my UIButtons as weak and not strong - I was under the impression that UI elements needed to be weak as they go off view so often?
I came to this answer with the help from the comments section.
This also does not explain why it works on iPhone 4S and iPhone 5 when declared as weak. It also worked on the 64 bit simulator declared as weak
Does this mean the 64bit simulator is to be used as a guide and actual testing must be done on the device as it seems the simulator is not accurate when it comes to UIKit testing?
I'd love to know more about this.
The problem was that [UIButton buttonWithType:UIButtonTypeCustom] doesn't leave an instance of the object as is stated in the Apple Docs. And the UIBarButton expects an instance of some sorts. https://developer.apple.com/library/ios/documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html
The solution as is stated in the AppleDocs is using the alloc+init construction to make a button.
buttonWithType:
"This method is a convenience constructor for creating button objects
with specific configurations. If you subclass UIButton, this method
does not return an instance of your subclass. If you want to create an
instance of a specific subclass, you must alloc/init the button
directly."
#TotumusMaximus You have misunderstood what the documentation is saying. Note the first part:
If you subclass UIButton...
What this means is that if you try to do [MyAwesomeButton buttonWithType:UIButtonTypeCustom], the object that will be created will be a UIButton instance, NOT a MyAwesomeButton instance. In order to get a MyAwesomeButton instance, then you would need to do [[MyAwesomeButton alloc] init]. There are no button subclasses involved in the OP's problem, so this portion of the documentation does not apply here.
If your properties are weak, and you directly assign a newly created object to them, then by the rules of ARC they should immediately be deallocated and set to nil, since nothing else holds a strong reference to them. The fact that is was working on other devices / simulators was an accident of implementation.
There is no real need to use weak properties for UI elements now that views are not unloaded under memory pressure conditions any more. If you do still want to use weak properties, the correct pattern to use is to create a local (strong) reference, add it to a superview, and then assign to the property - at this point the button will have an owning reference (the superview) so will not be removed by ARC:
UIButton *button = [UIButton buttonWithType:whatever];
[self.view addSubview:button];
self.button = button;

A UIButton above a UIPickerView

I have a UIPickerView which works correctly, now I want to add a button above it so that I can dismiss it.
and here is my code where I initiate a UIPickerView as well as its dismiss button:
- (UIPickerView *)creatPickerView {
UIPickerView *tempPickerView = [[[UIPickerView alloc]
initWithFrame:CGRectMake(kPickerViewX, kPickerViewY, kPickerViewWidth, kPickerViewHeight)] autorelease];
tempPickerView.showsSelectionIndicator = YES;
tempPickerView.delegate = self;
tempPickerView.dataSource = self;
UIButton *pickerButton = [[UIButton alloc] initWithFrame:CGRectMake(270, -32, 50, 32)];
[pickerButton setBackgroundImage:[UIImage imageNamed:#"hidePicker.png"]
forState:UIControlStateNormal];
[pickerButton addTarget:self action:#selector(hidePicker)
forControlEvents:UIControlEventTouchUpInside];
[tempPickerView addSubview:pickerButton];
[pickerButton release];
[self.view addSubview:tempPickerView];
return tempPickerView;
}
and it works well on my iPhone 4.3 Simulator, like this:
apparently there is a button on the upper right of the pickerView,
problem is, when I run the app in my device - a 5.0.1 iPhone4 and a 4.2.1 iTouch, the button is missed like it has never been added to the pickerView.
Can anyone help me with this?
Thanks a lot and a lot!
I found the reason, it seems the png has some problem,
after I change another png, it comes up in the screen!
but the real problem is that I place the button outside of the pickerView which results in the button's untouchableness.
But anyway the pickure is only a small problem.

Resources