UIButton Title In .xib Overriding Programmatic Title Change - ios

I am writing an app which loads a view (designed in a .xib) with multiple buttons. When loading the view I set the buttons titles programmatically. I then add the view as a subview. For a fleeting second I see the programatically set titles appear and then they change to the prototype titles in the .xib.
Here is the code:
self.cellPhoneButton.titleLabel.text = person.cellPhone;
self.homePhoneButton.titleLabel.text = person.homePhone;
// Do other stuff with the view
[self.peopleView addSubview:self.personView];
Why do the titles of the buttons revert back to their default content, and why is this not happening to the other components of the page (Labels, Navigation Bar Title, etc.)?
EDIT:
I also tried this code without success:
[self.cellPhoneButton setTitle:person.cellPhone forState:UIControlStateNormal|UIControlStateSelected];
[self.homePhoneButton setTitle:person.homePhone forState:UIControlStateNormal|UIControlStateSelected];
// Do other stuff with the view
[self.peopleView addSubview:self.personView];

You should use [self.homePhoneButton setTitle: forState:]; method to set the UIButton's title. and the state in your case should be UIControlStateNormal which means control state will apply to all the states if you don't specify the others explicitly. If you combine the Normal with others, then Normal will be overidded.

Related

UIButton default tap animation inside UITableViewCell

I have a few UIButtons inside UITableViewCells, and all of them are missing default tap animation, however if I long press - animation works. I've found some solutions, like setting type to .system, showsTouchOnHighlight = true, but none of them have helped. What is the problem here?
It's not a "problem" - it's by design.
When you have an object such as a button in a table view or scroll view or collection view, and you "touch" it, the system needs to know whether you are tapping the button or if you want to touch-and-drag to scroll the view that contains the button.
So, the table view (really, the "scroll view part" of the table view), waits to see if you drag your finger or not before it performs any actions.
If you want the button to respond immediately to a touch, you need to set
delaysContentTouches = false
on the containing scroll view(s).
If you search for uibutton inside uitableviewcell delaysContentTouches you should find plenty of discussion on this topic, and various approaches to change the default behavior.
For this problem you can add extension to UIButton and override some methods.
You can check my answer here

Replace views in detail part of the screen after user's action

I am new to Swift and app development. I have a design question. I am trying to make a view that contains a slider, but that as soon as the "touch up inside" action is performed, is replaced by a progress bar + button. If the button is pressed, then we go back to showing only the slider. This view will be not take the whole screen, only part of it.
What would be the best way of doing this? I have already investigated several options:
1. using a navigation controller with a segue triggered by the slider that goes into a new scene with a progress bar & button.
2. creating a custom view with two properties: a slider and a custom view (progress bar & button). The slider can be laid out using interface builder, and the custom view can be loaded from a nib file when needed.
3. creating a custom view with two properties: a slider and a custom view (progress bar & button). The new progress bar and button are created programmatically whenever the action is triggered on the slider.
I have already tried options 1 and 2 to some extent with no success. Since I am a beginner, I am trying to use the IB as much as possible. What is the best option (if any) from the list?
You can do this directly on the Storyboard without needing to create a custom view class, but you'll need a few lines of code in any case. Just drag a Slider into your View, and then drag a button and a progress view directly on top of that. Now select the button, and in the Attributes inspector, tick the box next to "Hidden". Do the same with the progress bar. Then just open the assistant editor and connect references to all 3 of those. You'll also need to create an action for the button (I've called it change), and make sure you leave the type field as AnyObject. Add the following line inside ViewDidLoad:
slider.addTarget(self, action: Selector("change:"), forControlEvents: .TouchUpInside)
This line just makes it so that change gets called anytime the user uses the slider. Obviously change slider to whatever you name your UISlider. You can implement the change function like this:
#IBAction func change(sender: AnyObject) {
slider.hidden = !slider.hidden
button.hidden = !button.hidden
progressBar.hidden = !progressBar.hidden
}
This is a simple implementation that just toggles between true and false for each of the items, but you'll probably want to do it differently depending on what this project does.
Now, if you want to put this functionality in multiple places in your app it might be easiest to create a custom view using the same concept as above, in which case check out this tutorial on how to create an IBDesignable UIView.

Changing UIToolBar (appearance) at runtime

I want to change an UIToolBar at runtime. In it's initial state, is has only one button, when that button is pressed i want it to change it's appearance to show 4 buttons. One of these buttons should cause the first UIToolBar to reappear.
Im seeing two approaches:
1) Have two UIToolBar nibs, and load them as needed.
2) Having all buttons on the first UIToolbar, and hide/show them as needed.
What would be the correct approach?
Personally, I would want to see all 4 button at initial launch with only relevant button in enabled state and rest in disabled state. Once I tap on the already enabled button I should see other buttons getting enabled. This is less surprising UI for end user. However, you can also go with #2 mentioned above in which case you might want to add some animation effect for better user experience.
The second approach would be better, because if you want to add more buttons tomorrow, you need to maintain 2 nib files instead of one.
But, think again is creating toolbar in xib file good solution?
I would create custom toolbar extending UIToolbar class and make 2 methods in it:
-(NSArray*) toolbarButtonsInitial;
-(NSArray*) toolbarButtonsExtended;
-toolbarButtonsInitial method returns UIBarButtonItems for initial state
-toolbarButtonsExtended method returns UIBarButtonItems for second state.
IMHO, this way has several advantages:
Your xib file doesn't have hidden buttons, or some button above other
one
If you need to add or remove some buttons you can do that easily for
each state
You can easily reuse this toolbar on other screens and create new
states if necessary

How to make the title label to be fit within the back button of UInavigation controller :XCode

I'm using navigation controller in one of my apps, and I am troubling with the title of navigation controller back button. I want
solution 1: to make the title label to be fit within the back button area (something like:adjustsFontSizeToFitWidth=YES)
or
solution2: to make the back button large enough to hold the text.
(solution 1 is more preferable).
For more info, I'm giving an image here...
fig1: Good for short titletext.
fig2: Title get truncated for long text.
fig3: Make the text self adjustable.
fig4: Make the button large enough to hold the text.
Thanks in advance... :)
In IOSX ,till now apple not providing the permissions for customising the back button in navigation controller ,for achieving your requirement you can use your custom button because we are not having permissions for changing the back button frame.
put below code in you .m file
button will auto resize with title
-(void)viewDidAppear:(BOOL)animated
{
self.navigationController.navigationBar.backItem.title = #"Back";//put here your title
}
you will be succeed

ios navigation buttons

I'm doing a custom nav in ios for the first time. I have six buttons laid out in a row. When I tap the button, I want the image to change. However, the button is not togglable. The only way a button can be unselected is if another button is touched. Only one button can be active at any given time.
My idea:
use UIButtons
change UIButton image on touch
keep track of the active button inside the navigation class
when an inactive button is touched, make the currently active button inactive and turn the touched button to active
I want the end product to work like a custom TabBarController, but without switching layouts. I just want to edit the content in the current ViewController.
Is there a better way to do this?
You could just use the UISegmentedControl, which has that functionality already. If you need to significantly customise the look and feel though, your UIButton solution sounds fine.
I setup the UIButtons in the Interface Builder like so:
Default with unselected background image
Disabled with selected background image
On touch, a button is self.enabled = NO which makes the UIButtons look change. However, the button goes dim, so I also implemented self. adjustsImageWhenDisabled = NO.
This way a button can't be re-selected once it's "active".

Resources