I have a button I'm setting its corner radius like this in ViewDidLoad():
self.myBtn.layer.cornerRadius = self.myBtn.frame.size.height / 2
self.myBtn.layer.masksToBounds = true
The result is great on the iPhone X. Screenshot:
But for some reason in devices that has smaller screens (like iPhone SE) the corners are really sharp and the result isn't what I want to achieve. Screenshot:
Does anybody know why is it happening?
Thanks!
Try to call your code in viewDidAppear (or better in viewDidLayoutSubviews)
It looks like you're using Autolayout. And it seems your button's layout depends on view's layout.
The problem is: the size of viewController's view may be incorrect when viewDidLoad called (but it's corrected before viewDidAppear called).
Related
layoutIfNeeded is not working properly in iPad but it is working on iPhone. I want to a circular button. On the iPhone the circle is generated properly but in iPad it looks like a rhombus shape.
_btnthur.layer.cornerRadius = _btnthur.frame.size.width/2;
[self._btnthur layoutIfNeeded];
Expected Result :
Current Output:
I can see two main issues here:
In your code here you are setting the cornerRadius on btnthur, but telling btnSat to layout.
You should not be calling layoutIfNeeded, you should be calling setNeedsLayout and allow the run cycle to coalesce layout calls for performance.
Hope this helps :)
Edit for updated question:
Your corner radius is wrong in the second one, it is much too large. Can you check that the frame is correctly set on the button at the point you are setting the corner radius?
You may want to move your code
_btnthur.layer.cornerRadius = _btnthur.frame.size.width/2;
[self._btnthur setNeedsLayout];
into layoutSubviews or viewDidLayoutSubviews (depending on view or view controller) and make sure that the corner radius is always updated when the frame (or more importantly, the size) is set.
In my iOS swift app I have an instance where I am making a circle with SKShapeNode(circleOfRadius: 15) but it makes a skinny ellipse instead, the width is only half of what it should be to make a circle.
Also, when I try to put something in the middle of the screen by setting the position.x of the object to view.bounds.width/2 it puts it in the middle of the left half of the screen instead of the middle of the whole screen. When I try to put it on the far right of the screen by using position.x to view.bounds.width it then goes to the middle instead of the right bound of the screen.
Has anyone ever seen this and know what the issue is?
Check the size of the view which you are adding the SKShapeNode to. It may be that your scene was created to half of the screen size.
Theres a few things you can checkout here first.
i didnt add the view's view controller as a child view controller before loading the view. That fixed this issue for me.
Check the frame of the superview before you're adding the view as its subview, when i seen the frame isnt what i thoguht it was number one solved my problem
the last thing i can think of is add this code to layout subviews. Layout Subviews is called once all the on screen view's bounds and frames have been updated.
If all three doesnt work. Then smoewhere in your code your not doing something right. Displaying some code that you have wrote can dramatically help us help you. Good Speed my friend God Speed.
I'm using auto layouts and custom layout for my collection view. There were a lot of auto layout warnings initially but I managed to remove all those warnings. Now the views are laid out fine but one of the subviews (a UIButton) of my collection view's cell is behaving weirdly. I have to make this button circular by setting its layer's cornerRadius based on the new frame of the button. Here's the code:
The frame of this button on storyboard is (8, 6, 52, 52). The debugger output looks something like this:
You can see that the corner radius of the button's layer is being set to half the height of the button but the resulting view is not what I expect it to be.
The left grey icon is the view I'm talking about. One can see that it is not circular despite having the corner radius set to half its height.
I have been trying and looking around for a solution for quite a while now. Any help would be appreciated.
Thanks
Doing some diagnostics (see the chat session), we confirmed that the issue was not in the view hierarchy nor the cornerRadius nor anything like that, but a simple border in the underlying image.
This issue is really frustrating and getting me crazy.
I've read everything about AutoLayout and so on, but it happens all the time.
My interface has a view with an image view as background and a smaller subview on top of it (corresponding to a field in my image). I've tried everything but it looks like the subview does not scale accordingly with my image when switching through devices.
By the way, the IB preview does not show what i real get when running the iOS Simulator, is that normal?
Why? What do i do wrong? I'm not sure what else to try!
http://i58.tinypic.com/34xqtc6.png
EDIT: i cannot post images :(
I have setup a githubrepo regarding your case, kindly have a look
https://github.com/usamaiqbal83/TestingProject
Constraints for Background Image View:
Constraints for Front View
the most important thing to remember is ofcourse the constraint equation
FirstItem.Attribute1 = (SecondItem.Attribute2 * Multiplier) + Constant
Instead of subview use Container View.
My goal was to make a View of buttons and have it grow with screen size so it works on all devices, filling 90% of the width of the screen. I am not using constraints as it is an older program, and turning on constraints would be too big a job right now. I was hoping to "cheat" my way around this. So, I tried this:
I have a smaller View created inside a full screen View (created in interface builder) that is basically a standard "view" item. Inside that smaller View are a bunch of UIButtons that make my "keyboard."
I would like to scale the smaller view (which visually scales the buttons too), and still have the buttons respond properly as they change size. So I have done this:
Setup:
View (buttonView) with UIButtons inside that view. The view and the buttons are attached (in Interface Builder) to IBOutlets and IBActions as necessary. The following code scales the view to match the screen:
_keyboardCard.transform = CGAffineTransformIdentity; // reset to normal
CGFloat keyboardWidth = _keyboardCard.bounds.size.width;
// screenWidth is set earlier and has width bounds value for full screen
CGFloat widthScale = (screenWidth/keyboardWidth)*0.9; // 90% of full screen width
_keyboardCard.transform = CGAffineTransformScale(CGAffineTransformIdentity, widthScale, widthScale);
This actually works visually just fine, and the "keyboardCard" grows in size, as do the buttons inside (visually), just like I expected. It looks good as I change from device to device.
But there are times when some of the buttons stop responding. SOME continue to work just fine, especially near the center, while others just stop. The touch failures start on ALL the edges, and work their way inward as the size grows. It is almost as if there is a "mask" stopping the buttons from working on the edges.
So the question is: Is this a legitimate ways to do this? It does not seem to be documented anywhere. And if not, what does one suggest as a way to make a view with buttons scale up and down and still have the buttons work as expected?
Or perhaps I am just better off making a small, medium, and large View of the "keyboard", and just pick the closest one based on screen size?
Suggestions and comments please. Thank you.