I have a button in my ios project which displays edit profile if the user is the current user and follow if the user is not the current user.
I am setting the titleLabel text programmatically dependant on the above condition. I am also programatically setting the button text to align center.
The problem I'm having is it works perfectly for the edit profile button which appears as it should center aligned but when showing the follow button the word follow is slightly off center to the left and I can't figure out where I'm going wrong.
Here is my code:
if([userID isEqualToString:credentialID]){
self.followEditBtn.layer.cornerRadius = 2;
self.followEditBtn.layer.borderColor = UIColorFromRGB(0xA6B9C1).CGColor;
self.followEditBtn.layer.borderWidth = 1.0f;
self.followEditBtn.layer.backgroundColor = UIColorFromRGB(0xF8FBFC).CGColor;
self.followEditBtn.titleLabel.textColor = UIColorFromRGB(0xA1B1C3);
self.followEditBtn.titleLabel.text = #"Edit Profile";
self.followEditBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
} else {
self.followEditBtn.layer.cornerRadius = 2;
self.followEditBtn.layer.borderColor = UIColorFromRGB(0x19A548).CGColor;
self.followEditBtn.layer.borderWidth = 1.0f;
self.followEditBtn.layer.backgroundColor = [UIColor whiteColor].CGColor;
self.followEditBtn.titleLabel.textColor = UIColorFromRGB(0x19A548);
self.followEditBtn.titleLabel.text = #"Follow";
self.followEditBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
}
And some images of the buttons as you can see the follow text is slightly off center to the left yet the edit profile button is correctly aligned:
I couldn't reproduce your problem (iOS 6.1 to 7.1). Although I had to make a small change in order to make it work, since it wasn't showing any text.
Instead of using:
self.followEditBtn.titleLabel.text = #"Edit Profile";
you should use:
[self.followEditBtn setTitle:#"Follow" forState:UIControlStateNormal];
I'm assuming you're using a UIButtonTypeCustom type UIButton, and having a fixed frame size.
I'd like you to tell me where are you putting this code, and how is this button being initialized.
Related
see above attached image.
It's programatically written in Objective-C. UISegmentController is cutting title text.
Weird thing is, it shows 'Privacy Policy' full text which is way longer than 'About' and 'Terms', which is cutting.
Can anybody please help in this?
code is given below:
UISegmentedControl *unitsPicker = [[UISegmentedControl alloc] initWithItems:#[#"About", #"Terms", #"Privacy Policy"]];
unitsPicker.frame = CGRectMake(0, CGRectGetMaxY(unitsLabel.frame) + kBottomPaddingConstant, CGRectGetWidth(_unitsContainer.frame), segmentHeightConstant);
unitsPicker.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
unitsPicker.selectedSegmentIndex = ![TemperatureUtility unitIsCelsius];
[unitsPicker addTarget:self action:#selector(onUnitPickerChanged:) forControlEvents:UIControlEventValueChanged];
unitsPicker.layer.cornerRadius = 5.0f;
unitsPicker.layer.borderColor = [UIColorFromRGB(0xE9E9E9) CGColor];
unitsPicker.layer.borderWidth = 1.0f;
unitsPicker.layer.masksToBounds = YES;
unitsPicker.clipsToBounds = YES;
[_unitsContainer addSubview:unitsPicker];
Try setting:
segmentedControl.apportionsSegmentWidthsByContent = YES;
From the docs:
If the value of this property is true, for segments whose width value is 0, the control attempts to adjust segment widths based on their content widths.
The default is false.
Swift 5 and 5+ Solution
Add
mySegmentController.apportionsSegmentWidthsByContent = true
inside viewDidLoad() {}
I have following settings for my textfield
_urlSearchTextFieldFirst.backgroundColor = [UIColor clearColor];
_urlSearchTextFieldFirst.placeholder = #"url";
_urlSearchTextFieldFirst.delegate = self;
_urlSearchTextFieldFirst.keyboardType = UIKeyboardTypeURL;
_urlSearchTextFieldFirst.keyboardAppearance = UIKeyboardAppearanceDefault;
_urlSearchTextFieldFirst.returnKeyType = UIReturnKeyGo;
_urlSearchTextFieldFirst.autocorrectionType = UITextAutocorrectionTypeNo;
_urlSearchTextFieldFirst.autocapitalizationType = UITextAutocapitalizationTypeNone;
and the keyboard its displaying looks some old way some way zoomed.
You need to add iPhone X launch screen in asset folder .
Then Keyboard will work properly
And For layout
You need to use safe layout , Go to storyboard , Select ViewController to make safe layout which is compatible for iPhone X
The answer to my question was hinted at in this question, so I think the answer is to disable autolayout for my UIToolbar view.
The code that is said to work for views is
cButton.translatesAutoresizingMaskIntoConstraints = YES;
But I’m not sure if it applies to my code since UIToolbar doesn’t inherit from UIView.
I have lots of small images that I use in my games that are different sizes depending on the device and orientation. Rather than having lots of different images, and adding new ones when Apple introduces new devices, I decided to make one 160x160 image fore each and then resize it when it is used. This worked fine from iOS 4 - iOS 10 but fails in iOS 11.
The code is pretty straightforward:
// Get the image
NSString *pictFile = [[NSBundle mainBundle] pathForResource:#"Correct" ofType:#"png"];
UIImage *imageToDisplay = [UIImage imageWithContentsOfFile:pictFile];
UIImage *cImage = [UIImage imageWithCGImage:imageToDisplay.CGImage scale:[UIScreen mainScreen].scale orientation:imageToDisplay.imageOrientation];
UIButton *cButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cButton setImage:cImage forState:UIControlStateNormal];
[cButton setTitle:#"c" forState:UIControlStateNormal];
//set the frame of the button to the size of the image
cButton.frame = CGRectMake(0, 0, standardButtonSize.width, standardButtonSize.height);
//create a UIBarButtonItem with the button as a custom view
c = [[UIBarButtonItem alloc] initWithCustomView:cButton];
This is what it looks like pre11. The bar button items have been resized and fit nicely in the bottom bar. Note I reduced the size of the checkmark by 50% just to make sure I was looking at the correct code and that it behaves like I expect.
Here’s what they look like in the simulator for Xcode 9.0 GM and iOS 11. Note that the top row of buttons resize correctly but the bottom row expand to fill the space allocated for the tab bar. The same behaviour happens on iPads as well and various devices.
Any ideas about how to disable Autolayout or add constraints?
BarButtonItem (iOS11\xCode9) uses autolayout instead of frames. Try this (Swift):
if #available(iOS 9.0, *) {
cButton.widthAnchor.constraint(equalToConstant: customViewButton.width).isActive = true
cButton.heightAnchor.constraint(equalToConstant: customViewButton.height).isActive = true
}
Objective C
if (#available(iOS 9, *)) {
[cButton.widthAnchor constraintEqualToConstant: standardButtonSize.width].active = YES;
[cButton.heightAnchor constraintEqualToConstant: standardButtonSize.height].active = YES;
}
Fallstreak answer (+1) was the solution in my case. I was having a custom view not work on tap. Just wanted to share what I had to do for a custom view in a navigation item to work. This is all swift 3
let backButtonView = BackButtonView.loadNib()
backButtonView?.addTarget(self, action: #selector(returnToPrevious), for: .touchUpInside)
backButtonView.widthAnchor.constraint(equalToConstant: backButtonView.frame.width).isActive = true
backButtonView.heightAnchor.constraint(equalToConstant: backButtonView.frame.height).isActive = true
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backButtonView!)
The 2 lines about the anchor width/height came from Fallstreak's answer and were the solution in this case.
I have a UIButton, and I would like to access the UIImageView of its background image so that I can make the image circular. I know that I can affect the image itself but I would prefer to do this more elegantly. I also know that I can use the button.currentBackgroundImage property to get the UIImage in the background, but I want the view itself. Once I have the view I intend to use this code:
buttonImageView.layer.cornerRadius = buttonImageView.frame.size.width / 2;
buttonImageView.clipsToBounds = YES;
How can I access the buttonImageView?
EDIT:
Due to #vhristoskov's suggestions, I tried cropping the button itself with this code:
self.button.layer.cornerRadius = self.button.frame.size.width/2;
self.button.clipsToBounds = YES;
And it made this shape:
After debugging I found that frame.size.width was 46, when it should have been 100. So I tried this code instead:
self.button.layer.cornerRadius = self.button.currentBackgroundImage.size.width/2;
self.button.clipsToBounds = YES;
And that produced this shape:
And this time, the cornerRadius was being set to 65. So it actually seems like my problem is that I don't have the correct width at the moment. Which property should I access to get the correct number?
Well as I guessed and as you've already found - the problem is in the button size. To be sure that your button's size at runtime is what you expected to be - review your constraints. In the example below the button has vertical and horizontal central alignment and fixed width and height.
Constraints:
To have perfectly circular button you should have button.width == button.height. If this condition is met your code should work like a charm:
self.button.layer.cornerRadius = CGRectGetWidth(self.button.frame) / 2;
self.button.clipsToBounds = YES;
Assuming that you called something like:
[self.myButton setBackgroundImage:[UIImage imageNamed:#"image"] forState:UIControlStateNormal];
in viewDidLoad or earlier, you can then call the following in viewDidAppear:
if (self.myButton.subviews.count > 0 && [self.myButton.subviews[0] isKindOfClass:[UIImageView class]]) {
UIImageView *imageView = self.myButton.subviews[0];
imageView.layer.cornerRadius = imageView.frame.size.width / 2;
imageView.clipsToBounds = YES;
}
but it is not more elegant as the subview ordering is an implementation detail of UIButton and may change at any time.
So I have a strange issue which could be a bug or me being stupid.
I have a button in a cell. The cell shows the logged in users facebook friends. If they are already a user of the app it shows the button as Play, else it shows Invite Me...
UIButton *playButton = (UIButton *)[cell.contentView viewWithTag:30];
playButton.backgroundColor = [UIColor clearColor];
[playButton setBackgroundImage:[[UIImage imageNamed:#"fbCell_Invite_30h"] resizableImageWithCapInsets:UIEdgeInsetsMake(15, 5, 15, 5)] forState:UIControlStateNormal];
playButton.titleLabel.text = #"Invite me";
if ([self.fbFriendsUsingApp containsObject:[friend valueForKey:#"id"]]) {
playButton.titleLabel.text = #"Play";
}
This all works fine and not an issue. However, when the button text is invite me it shows as this:
In storyboard interface builder the button is setup like this:
What is strange is that if I click the middle arrow <------> to make the width stretch, the title text stops working and they all show 'Play' ???
Try setting the text of the button using
[playButton setTitle:#"Invite Me" forState:UIControlStateNormal];
instead of
playButton.titleLabel.text = #"Invite Me;
I've found that if I don't use the first approach above the changes to the button text don't show up.
You can find the size of the NSString that you are setting as the button label and then resize the button accordingly:
CGSize labelSize = [labelString sizeWithFont:[UIFont systemFontOfSize:12]];
[button setFrame:CGRectMake(10,0,labelSize.width, labelSize.height)];
You will probably want to add a little to the CGSize as padding, but play with it and find the result you desire.