A UIButton above a UIPickerView - ios

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.

Related

UIButton retains the glows after it has been touched

Hi i am making customUIButtons in my app by using the following piece of code.
+ (NSArray *) createButtonItemNormalImage:(UIImage *)normalImage
highlightImage:(UIImage *)highlightImage
disabledImage:(UIImage *)disabledImage
touchUpSelector:(SEL)selector
target:(id)target
{
// HighlightImage is not used. Highlight is shown using iOS glow
UIButton *uiButton = [UIButton buttonWithType:UIButtonTypeCustom];
uiButton.bounds = CGRectMake(0,
0,
normalImage.size.width,
normalImage.size.height);
[uiButton setImage:normalImage
forState:UIControlStateNormal];
if (disabledImage)
{
[uiButton setImage:disabledImage
forState:UIControlStateDisabled];
}
[uiButton addTarget:target
action:selector
forControlEvents:UIControlEventTouchUpInside];
uiButton.showsTouchWhenHighlighted = YES;
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:uiButton];
return [NSArray arrayWithObjects:buttonItem, uiButton, nil];
}
I have made a cancel button using the above function. The cancel button takes the user from one screen to another screen. The problem is when i come back to the first screen the cancel button is still glowing. I have seen this problem before also but a call to [self.view setNeedsLayout] used to solve it.
Why does it happen and what would be a correct way of solving it?
Thanks!
To solve this problem in not so standard way I now set the highlighted state to no for all the buttons when I enter the first screen. I use myButton.highlighted = NO;. However the documentation says following for highlighted property.
Specify YES if the control is highlighted; otherwise NO. By default, a control is not highlighted. UIControl automatically sets and clears this state automatically when a touch enters and exits during tracking and when there is a touch up.
Its not happening so in my case.I would love to know the reason behind it and standard ways of solving it

Popover Control Background Color Odd Behavior

So in my app I have a popover control with an embedded navigation control. In different parts of the navigation stack, I want the popover to be different colors depending on where the user is. The weird thing is sometimes setting the popover background color makes this terrible looking box around it, sometimes it doesn't. It looks like this:
This is the look I am trying to get:
It seems if I change the background color before displaying the popover it seems to work and transition correctly, but if I don't set the popover color before showing it, then change it after it has been shown it has the box effect. I've also noticed other cases where it seems to happen randomly, but I can't really explain what is causing it (my real app is much more complex than this demo). Here is the relevant code:
- (IBAction)buttonPressed:(id)sender {
UIViewController *vc = [[UIViewController alloc] init];
UIButton *b = [[UIButton alloc] init];
[b addTarget:self action:#selector(innerButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[b setTitle:#"Button" forState:UIControlStateNormal];
[b setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[b setFrame:CGRectMake(0,0,100,100)];
[vc.view addSubview:b];
_innerNav = [[UINavigationController alloc] initWithRootViewController:vc];
_popOver = [[UIPopoverController alloc] initWithContentViewController:_innerNav];
//If this line is here, everything works fine
_popOver.backgroundColor = [UIColor yellowColor];
[_popOver presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
//If this line is here (and the above line is commented out), the transition will look wrong
//_popOver.backgroundColor = [UIColor yellowColor];
}
-(void)innerButtonPressed {
_controller = [[UIViewController alloc] init];
UIButton *b = [[UIButton alloc] init];
[b addTarget:self action:#selector(test) forControlEvents:UIControlEventTouchUpInside];
[b setTitle:#"Make Purple" forState:UIControlStateNormal];
[b setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[b setFrame:CGRectMake(0,0,200,200)];
[_controller.view addSubview:b];
[_popOver setBackgroundColor:[UIColor orangeColor]];
[_innerNav pushViewController:_controller animated:YES];
}
-(void)test{
_popOver.backgroundColor = [UIColor purpleColor];
}
Any idea what is causing this issue? And what steps to safely update the background color of a popover without ever getting into this state? I have a full project demonstrating the problem, I thought you could attach projects to questions but apparently you cannot. If someone wants it I can probably host it somewhere.
After looking at your sample project, Apple's "Popover Controllers in iOS" sample project, perusing Apple's Documentation, and trying a few different things I have come to the following observations:
The UIPopoverController only exhibits this odd behavior when it is presented without a valid value for the backgroundColor property. From this I am guessing that since UIPopoverController's backgroundColor property is nil by default it must use different drawing code than when the backgroundColor property is valid.
Triggering some sort of redraw (e.x. Setting popoverContentSize) will get the colored box overlay to go away (it looks like it clips a color layer).
Conclusion: For the time being I would set a backgroundColor prior to the UIPopoverController being presented and then update it as needed. If this is not an option try updating the UIPopoverController such that it redraws (As a note: I was not able to get this to look good and it seems hacky). Lastly, I would report it as a bug to apple.
I hope this helps.
UIPopoverController is now deprecated. I found a similar issue when updating it to use the new popoverPresentationController. In the past I was able to set the backgroundColor of UIPopoverController before presenting. The popover presentation controller also has a backgroundColor property but didn't work like it did before where I could assign it before presentation. To get it to work I had to assign it after it starts presenting for some reason:
contentViewController.modalPresentationStyle = UIModalPresentationPopover;
[[self presentViewController:contentViewController animated:YES completion:^{
// completion code
}];
contentViewController.popoverPresentationController.backgroundColor = [UIColor orangeColor];
For your particular scenario where you are changing the background color after presentation is finished I don't think you'd be able to do that by just changing the popoverPresentationController's backgroundColor. The only solution I can think of is to dismiss and re-present the popover without animating:
[self dismissViewControllerAnimated:NO completion:^{
contentViewController.modalPresentationStyle = UIModalPresentationPopover;
[[self presentViewController:contentViewController animated:NO completion:^{
// completion code
}];
contentViewController.popoverPresentationController.backgroundColor = [UIColor purpleColor];
}];

Come back to app from an other app

Hello,
When we are phoning with us iphone and you left the call view to come back at springboard, we can come back to the call view with the status bar. (This picture can better explain : http://what-when-how.com/wp-content/uploads/2011/08/tmpB178_thumb.jpg)
Or, in the Facebook app when you go to the messenger Facebook app (not same app) we can touch status bar to comme back Facebook App too.
I would like to know if it's possible to make it in my app ? And if it's possible, how I will proceed ? (Edit: I want co come back in my app from another app such Youtube.)
Thanks
Once you become familiar with how to open another app within your current app form following link:
http://iosdevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html
You can simply create a view that has tap gesture and use it as a button
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:YES];
UIView *tapToReturnButton = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
tapToReturnButton.backgroundColor = [UIColor blueColor];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(tapToReturnButtonClicked)];
[tap setNumberOfTouchesRequired:1];
[tapToReturnButton addGestureRecognizer:tap];
UILabel *tapToReturnLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 20)];
tapToReturnLabel.text = #"Tap to return";
tapToReturnLabel.textColor = [UIColor whiteColor];
tapToReturnLabel.textAlignment = NSTextAlignmentCenter;
tapToReturnLabel.font = [UIFont fontWithName:#"ArialMT"
size:14];
[tapToReturnButton addSubview:tapToReturnLabel];
[self.view addSubview:tapToReturnButton];
}
- (void)tapToReturnButtonClicked
{
NSLog(#"Now you add your code that opens another app(URL) here");
}
Edited:
After I posted the code above I kind of realized that there will be no tap gesture on the status bar even though other bottom part (20 pixel) of tapToReturnButton has a click gesture. After I did some research, I think following link has the better solution on click gesture. I will probably use tapToReturnButton as placeholder to let users know where to touch though and remove UITapGestureRecognizer *tap.
How to detect touches in status bar
Again, I think there is multiple way to achieve your need but those links above will give you good starting point.
URL schemes will allow you to launch an app from an another app.
Check the following links for more info:
https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/AdvancedAppTricks/AdvancedAppTricks.html#//apple_ref/doc/uid/TP40007072-CH7-SW18
https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/AdvancedAppTricks/AdvancedAppTricks.html#//apple_ref/doc/uid/TP40007072-CH7-SW50
here are some system supported URL schemes
https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/Introduction/Introduction.html#//apple_ref/doc/uid/TP40007899

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;

Button not showing on phone but yes on XCode simulator

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.

Resources