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.
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.
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.
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
I am adding a custom UIImageView to my UINavigationBar using this code:
UIImageView *background = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];
[background setImage:[UIImage imageNamed:#"nav_background.png"]];
[background setTag:kTagForReference];
[self.navigationController.navigationBar insertSubview:background atIndex:0];
I then add a custom title using this code:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 400, 44)];
...
[self.navigationItem setTitleView:label];
[label release];
But on some of my pushed views the title is being hidden (or isn't visible). I can't seem to push more than two views onto the stack without the title disappearing.
I've tried to force my UIImageView background to the back of the bar but that still doesn't help. I've printed the subviews of the UINavigationBar and I can see that the UILabel is there, I just can't see it!
Any help would be greatly appreciated.
If you can run the dumpWindows() function, the output would show the view hierarchy so you can see what view is covering it up.
You most likely want to use a UIToolBar instead of a NavigationBar. Then you can add the subviews to the UIToolbar.
Bring the UILabel to the front most view.
[self.view bringSubviewToFront: label];
I would like put UIButton on my UIViewController with style like have buttons in UIActionSheet. What should i add here:
UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];
[ button setFrame: CGRectMake(10, 10, 50, 35) ];
[button setTitle:#"Button from UIActionSheet" forState: UIControlStateNormal];
[self addSubview: button];
You need to get the background image from somewhere and then set it using: - (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state
I believe the .psd linked here has the action button background image http://media.photobucket.com/image/uiactionsheet%20button%20psd/visionwidget/iphone-gui-psd-file.jpg
you can using segmented control - very good trick
segmented = [[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Info",#"Configuration", nil]] autorelease];
[segmented addTarget:self action:#selector(changeView:) forControlEvents:UIControlEventValueChanged];
segmented.tintColor = [UIColor colorWithRed:0.20 green:0.20 blue:0.52 alpha:1.0];
segmented.segmentedControlStyle = UISegmentedControlStyleBar;
Maybe exists some simple way like set some property?
Here is a post on using Apple's private UIGlassButton class.
You can't submit an app like this to the app store, but you can capture the generated background image to reuse in your published apps.