i got this problem only in IOS 7 and before i have read question on Stack overflow but could not find the one.
i have buttons in my APP (UISegmented) like back button text is Back, but suddenly middle of operation or when another view load on top of view, bottom view back button will show text as Ba.. also other buttons will change in text with dots in it…
NSArray *itemArray = [NSArray arrayWithObjects: #"Back", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(0, 0, 60, 28);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
[segmentedControl setTintColor:[UIColor colorWithRed:19.0/255.0 green:62.0/255.0 blue:137.0/255.0 alpha:1]];
[segmentedControl addTarget:self
action:#selector(handleBackButton:)
forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *segmentBarItemLeft = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
i had came across similar question here but at present could not find the same.
Basically the problem is in your code segment control button name is not setting properly. So should try for setting the button title using below api:-
- (void)setTitle:(NSString *)title
forSegmentAtIndex:(NSUInteger)segment
Related
I am generating my custom view controller's view programmatically, including adding a few buttons. I have three buttons in a row, all created along the lines of this one:
self.futureButton = [[UIButton alloc] initWithFrame:CGRectMake(0, buttonOffset, _width/3, _height*.1)];
self.futureButton.backgroundColor = [UIColor pinkColor];
self.futureButton.tag = 1;
[self.futureButton setTitle:#"Upcoming" forState: UIControlStateNormal];
[self.futureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[self setColor:[UIColor lightPinkColor] forState:UIControlStateSelected forButton:self.futureButton];
[self.futureButton addTarget:self action:#selector(changAppt:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.futureButton];
I also do this at the end to apply the same properties to all these buttons:
for(UIButton *myButton in self.view.subviews){
if([myButton isKindOfClass:[UIButton class]]){
[[myButton layer] setBorderWidth:4.0f];
[[myButton layer] setBorderColor:[UIColor whiteColor].CGColor];
[myButton.layer setCornerRadius:12.0f];<----problem
}
}
Everything is working great except the last command, setCornerRadius. If I comment it out, I have what I want - all the buttons lined up, white border that disappears into the background white. However if I use this last command, setCornerRadius, I get the following (see image)
Also the weird effect is on whichever button is selected. Do I have to do something about the selected state effects and the CornerRadius? If so, what?
Please add
myButton.clipsToBounds = YES;
and this is it :)
If you are interested in more details read this.
I am setting a rightbarbuttonitem in from a viewController using the following code:
UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:#"Save" style:UIBarButtonItemStylePlain target:self action:#selector(saveDetails)];
self.navigationItem.rightBarButtonItem = item;
It is appearing as shown in the image:
As you can see, the button which appears is not vertically aligned with either th leftbuttonitem or the title.
I also tried adjusting the alignment using the code mentioned here:
[self.navigationItem.rightBarButtonItem setTitlePositionAdjustment:UIOffsetMake(0, -10) forBarMetrics:UIBarMetricsDefault];
But this removes the custom font and moves the button even higher.
How can I set a rightbarbuttonitem which is aligned AND maintains the custom font set using the UIAppearance proxy?
EDIT:
I even tried using
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:#selector(saveDetails)];
but the result is exactly as shown in the first image.
UPDATE (Sep 20):
It seems that I will have to go with Alexander's answer, anybody with a cleaner solution?
You can use the next variant:
UIButton *saveButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 20)];
[saveButton addTarget:self action:#selector(saveDetails) forControlEvents:UIControlEventTouchUpInside];
[saveButton setTitle: #"Save" forState:UIControlStateNormal];
[saveButton setTitleEdgeInsets:UIEdgeInsetsMake(5, 0, 0, 0)];
UIBarButtonItem *item =[[UIBarButtonItem alloc] initWithCustomView:saveButton];
self.navigationItem.rightBarButtonItem = item;
I tried the answer but didn't seem to work.
The solution for me was adding UIButton as UIBarButtonItem. If it sounds confusing, just drag a UIButton from Interface Builder to the position of Left or RightBarButtonItem on the navigationBar in your storyboard.
Hope it helps.
This has been covered disparately in other questions and answers but this is a more general request for advice.
I am writing a scientific App so the titles in the Navigation Bar have a habit of being long. So I need to contrict the size of the back image or text.
If I do this at the View Controller level it works perfectly but it means inserting the code in every VC. E.g.
UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 60.0f, 36.0f)];
UIImage *backImage = [[UIImage imageNamed:#"backButton.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 12.0f, 0, 12.0f)];
[backButton setBackgroundImage:backImage forState:UIControlStateNormal];
[backButton setTitle:#"" forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(popBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backButtonItem;
As I want this to be consistent what is the best approach in iOS 7 to put it in the App Delegate so it is only instigated once?
The best approach in my opinion would not be to put this in the App Delegate, It would be to create a subclass of uiviewcontroller and add it there in the viewwillappear than have all the view controllers in your app of "that" kind.
How can I recreate the UIButtons displayed at the top of the SMS app (such as the "Load Earlier Messages" button). They look like pretty standard UIButtons except with a gradient, a slight shadow drop shadow and inner shadow, and a bluer border. Does Apple use a stretchable UIImage to make these or are they created programmatically (and how)?
My favorite solution to this problem is to create a segmented control with exactly one segment. You end up with a nice-looking button. Here's how I do it programmatically:
UISegmentedControl* takePhotoButton =
[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Take Photo"]];
takePhotoButton.segmentedControlStyle = UISegmentedControlStyleBar;
takePhotoButton.frame = CGRectMake(55,336,110,38);
takePhotoButton.tintColor = [UIColor lightGrayColor];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:[UIFont boldSystemFontOfSize:16]
forKey:UITextAttributeFont];
[takePhotoButton setTitleTextAttributes:attributes
forState:UIControlStateNormal];
takePhotoButton.momentary = YES;
[takePhotoButton addTarget:self
action:#selector(onTakePhoto:)
forControlEvents:UIControlEventValueChanged];
[self.view addSubview:takePhotoButton];
I have a method that I want to get called when the user clicks on a button in the navigation bar. If I add the button like this then my method gets called:
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"myEditButton"]
style:UIBarButtonItemStyleBordered
target:self
action: #selector(enterEditMode:)];
editButton.title = #"Edit";
[self.navigationItem setRightBarButtonItem:editButton animated:YES];
When the user clicks on the button then my enterEditMode: method gets called.
However using this code the result looks like as in the attachment - there's no text but worse my image is lying on top of a blue button. I can't use the standard system edit button because there is a requirement for it to be colored black not blue and AFAIA I'm unable to change the color of the standard edit bar button to black?
So in a xib I created a parent UIView which contains a UIImageView which is the button image and a UILabel for the text.
Then I create the button item like this:
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithCustomView:self.editButtonParentView];
[editButton setTarget:self];
[editButton setAction: #selector(enterEditMode:)];
editButton.style = UIBarButtonItemStyleBordered;
editButton.target = self;
self.editButtonLabel.text = #"Edit"
[self.navigationItem setRightBarButtonItem:editButton animated:YES];
Where editButtonParentView is an IBOutlet to the partent view in the nib.
This displays perfectly, however the problem is that enterEditMode: does not get called when the user clicks on it.
Why does it not get called?
Thanks
This link is where I found out how you can get the same look as the system 'Edit' button but get any background color you want. Try this code:
// From:
#import "UIBarButtomItem+Tinted.h"
#implementation UIBarButtonItem (Tinted)
+ (UIBarButtonItem *)newBarButtonItemWithTint:(UIColor*)color andTitle:(NSString*)itemTitle andTarget:(id)theTarget andSelector:(SEL)selector
{
UISegmentedControl *button = [[UISegmentedControl alloc] initWithItems:#[itemTitle]];
button.momentary = YES;
button.segmentedControlStyle = UISegmentedControlStyleBar;
button.tintColor = color;
[button addTarget:theTarget action:selector forControlEvents:UIControlEventValueChanged];
return [[UIBarButtonItem alloc] initWithCustomView:button];
}
#end