Right navbar button moves when alertview is dismissed - cocoa

I'm adapting an iOS 6 app to iOS 7 and I'm experiencing an strange "error". In a screen there's a rightBarButtonItem with a simple image that is showed in his place. But, if the app shows an alertview, the image moves down (50 px or so) when I tap the OK button of the alertview (the only button in this alert). There's no action linked to this alertview, it's only informational.
Also, if I change the image (setImage) of the button, this image will appear out of place.

Well, I finally found myself a solution:
I had a UIBarButtonItem with UIBarButtonItemStylePlain and an image setted with setImage on the UIBarButtonItem.
To solve the issue, I have created an UIButton with the image (setting its frame with an CGRectMake) , and then I have created the UIBarButtonItem with initWithCustomView and using the UIButton as the CustomView. This way the image is always where it should be.
Edit:
UIButton* aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.frame = CGRectMake(0.0, 40.0, 30.0, 30.0);
[aButton setBackgroundImage:[UIImage imageNamed:#"anImage.png"] forState:UIControlStateNormal];
[aButton addTarget:self action:#selector(aFunction:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *anUIBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:aButton];
self.navigationItem.rightBarButtonItem = anUIBarButtonItem;

Thanks for this raul, I've translated it into Swift for the Swift users out there:
let a = UIButton(type: .Custom)
a.frame = CGRectMake(0.0, 40.0, 30.0, 30.0)
a.setBackgroundImage(UIImage(named: "Share")!, forState: .Normal)
a.addTarget(self, action: "shareThis:", forControlEvents: .TouchUpInside)
let uiItem = UIBarButtonItem(customView: a)
self.navigationItem.rightBarButtonItem = uiItem

I had a similar issue with my right nav bar button and it went away when I removed the "title" value of the offending bar button item. In my case the title should never have been set because the button uses an image. YMMV.
I'm not sure why it matters but it fixed my issue with the bar button item getting offset on uialertcontroller dismissal.
I got my inspiration from this question: UIAlertController moves leftBarButtonItem down

Related

How to reduce the space between uibarbutton and back button in ios?

I am trying to use an image adjacent to back button arrow with some little space.
i have used the code below ,but this have increased space between arrow and image. can anyone tell me how to reduce this space .
UIButton *button1=[UIButton buttonWithType:UIButtonTypeCustom];
[button1 setFrame:CGRectMake(-10, 2.0, 200, 42.0)];
UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithCustomView:button1];
button.imageInsets = UIEdgeInsetsMake(0, -100, 0, -25); //this didn't worked out as expected ...i used this for reducnig space
self.navigationItem.leftBarButtonItem = button;
//THis allows to add image / button to the right of back button
self.navigationItem.leftItemsSupplementBackButton = YES;

How do I find the reference/identifier of a button on the storyboard in xcode?

Thanks for reading; this is my problem, I'm trying to hook up code I have written to buttons which already exist in the storyboard but I can't find a reference or identifier for the button(s).
I want to be able to modify buttons which have been created on the storyboard and style/give actions in my code.
So I would like something I can reference like so:
UIButton *subBtn;
where subBtn is the button's reference/identifier.
tldr; where do you find the reference for buttons in xcode (v6+) storyboard?
For those who come after me:
UIButton *subBtn = (UIButton *) [self.view viewWithTag:9];
The Line above allows you to reference a button with an "tag" which you can find in the attributes tab in the storyboard.
By using this it allows you to change attributes like below, to format your buttons:
[subBtn addTarget:self action:#selector(submitHit) forControlEvents:UIControlEventTouchUpInside];
UIImage *UISubImg = [UIImage imageNamed: #"submit_button.png"];
UIImage *UISubImg2 = [UIImage imageNamed: #"submit-play-icon.png"];
[subBtn setTitle:#"SUBMIT" forState:UIControlStateNormal];
subBtn.frame = CGRectMake(20, 400, 280, 65);
[self.view addSubview:subBtn];
[subBtn setBackgroundImage:UISubImg forState:UIControlStateNormal];
[subBtn setImage:UISubImg2 forState:UIControlStateNormal];
SubmitHit is a method.

How to update a bar button item in a toolbar

I have a single playback controller in my app on the navigation bar and is easily found in its implementation file. So I've tried to add a toolbar and move the playback controls to that toolbar, but I don't know how to reference it programmatically and therefore cannot update the image used to indicate if music can be paused or played.
You can do this a couple of ways, one is by setting the image of the UIBarButtonItem not to be confused with the backgroundImage of a button. You can add a bar button like so:
UIBarButtonItem *barButton1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"camera.png"] style:UIBarButtonItemStylePlain target:self action: #selector(pressButton1:)];
The image should be a grayscale image somewhere around 30 x 30 in size much like a tab bar icon. Declare the bar button in the interface file and you can set the image like this:
[barButton1 setImage:[UIImage imageNamed:#"Chats.png"]];
The other way you can do this is by using a custom UIButton like so:
button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(0, 0, 64, 30);
[button1 setBackgroundImage:[UIImage imageNamed:#"camera.png"] forState: UIControlStateNormal];
[button1 setBackgroundImage:[UIImage imageNamed:#"Chats.png"] forState: UIControlStateSelected];
[button1 setTitle:#"Camera" forState:UIControlStateNormal];
button1.titleLabel.font = [UIFont boldSystemFontOfSize:12];
[button1 addTarget:self action:#selector(pressButton1:) forControlEvents: UIControlEventTouchUpInside];
UIBarButtonItem *barButton1 = [[UIBarButtonItem alloc] initWithCustomView:button1];
Declare button1 in the interface file and then you can change the image like so:
[button1 setSelected:TRUE];

UIButton Animation for IPhone

I am new to xcode, but I have been searching around for solution, and I have tried many different methods, but none of it is working. I am trying to make UIButton flip to the reverse side by adding animation to it. This will occur when program loads up, so user can just sit back and watch a whole bunch of buttons flip over and over.
The problem that I am having is that the animation seems to take place before screen even loads because I can see a different view (to show that button is flipped), as soon as the program loads, but I cannot see the animation taking place.
What I have done:
I created a main view controller and inside the controller I have different subviews and within those subviews, I have UIButtons. I tried to add a delay to my animation, but it still does not fix it. I don't know if the problem could occur because I create subviews and buttons and then animate under - (void)viewDidLoad. I also tried to create another subview and put just one button into that subview, which will create 2 subviews and each having just one button and animate by flipping between the subviews, but I still get the same problem. The animation still happened before the screen even loads up, instead of during the run time of the program.
I am attaching part of the code that I did to this one.
CGRect button20subviewFrame = CGRectMake(242, 370, 70, 83);
UIView *button20subview = [[UIView alloc] initWithFrame:button20subviewFrame];
UIButton *button20 = [UIButton buttonWithType:UIButtonTypeCustom];
[button20 addTarget:self action:#selector(switchToDetailView:) forControlEvents:UIControlEventTouchDown];
[button20 setTitle:#"Button20" forState:UIControlStateNormal];
button20.frame = CGRectMake(0 , 0 , 70 , 83);
UIImage *button20Image = [UIImage imageNamed:#"t1.png"];
[button20 setImage:button20Image forState:UIControlStateNormal];
[button20subview addSubview:button20];
[self.view addSubview:button20subview];
UIButton *button21 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button21 addTarget:self action:#selector(switchToDetailView:) forControlEvents:UIControlEventTouchDown];
button21.frame = CGRectMake(0, 0, 70, 83);
UIImage *button21Image = [UIImage imageNamed:#"t2.png"];
[button21 setImage:button21Image forState:UIControlStateNormal];
//animation part
[UIView animateWithDuration:3 delay:2 options:UIViewAnimationTransitionFlipFromLeft animations:^{
[button20 removeFromSuperview];
[button20subview addSubview:button21];
}completion:^(BOOL finished){
}];
Any help is appreciated.
Thank you.
viewDidLoad is the problem. It gets called when the view is done loading, which is before it is put on screen. viewDidAppear should do what you're looking for.
In addition to this, you may want to call [button20 removeFromSuperview] in the animations completion handler to ensure that the front of the button isn't removed while the animation is still taking place.
E.x:
[UIView animateWithDuration:3 delay:2 options:UIViewAnimationTransitionFlipFromLeft animations:^{
    [button20subview addSubview:button21];
}completion:^(BOOL finished){
if(finished){
  [button20 removeFromSuperview];
}
}];

Disable Subview of UITableview from Scrolling

(xCode 4.3.2, ARC, Storyboard) I have a Master/Detail project and the MasterViewController is a UITableViewController, as usual. It has a navigation bar at the top and a toolbar at the bottom. I've created a custom subview that I load programmatically in viewDidLoad.
It loads fine, and is on top just like I want, but when the user scrolls the tableview, the subview scrolls too. I need the subview to "stick" to the bottom.
Here is the code I used to add the subview:
CGRect totalFrame = CGRectMake(0, 314, 320, 58);
UIView *totalBG = [[UIView alloc] initWithFrame:totalFrame];
totalBG.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"totalBG.png"]];
[self.view addSubview:totalBG];
CGRect totalLabelFrame = CGRectMake(12, 12, 80, 40);
totalLabelBG = [[UILabel alloc] initWithFrame:totalLabelFrame];
totalLabelBG.backgroundColor = [UIColor clearColor];
totalLabelBG.text = #"Total:";
[totalLabelBG setFont:[UIFont boldSystemFontOfSize:22]];
totalLabelBG.userInteractionEnabled = NO;
totalLabelBG.textColor = [UIColor whiteColor];
totalLabelBG.shadowColor = [UIColor blackColor];
totalLabelBG.shadowOffset = CGSizeMake(0, 1);
[totalBG addSubview:totalLabelBG];
I tried setting userInteractionEnabled to NO in viewDidLoad and detecting a
touch, and setting the totalLabelBG.center property, and neither worked.
I've also read through a lot of threads on disabling scrolling for webviews, but found nothing relevant.
I found another SOF question with a response that may or may not work, but the user did not explain the answer. "The trick is to adjust the frame of the "non-scrollable" subview inside -layoutSubviews."
I achieved the desired result by adding the subview to the UINavigationController container like this:
[self.navigationController.view addSubview:totalBG];
Like in the answer above. Just add that particular subview to any subview outside on the UIScrollView instance. If its not suppose to scroll then it doesn't make sense to put it inside the scroll view to begin with. Instead make the scroll view only take up the height up to the top of the totals subview

Resources