Circular button becomes rounded rectangle after size increase
Auto Layout happens after viewDidLoad
, so you need to move your adjustment to viewDidLayoutSubviews
so that the frame size will be set to the correct size.
How to make Round Buttons for all screen sizes in swift?
Firstly, Remove the leading & trailing space from your button constraints & give your button the center of the view through constraints.
To create a perfect round shape(circle) the width and height of the button must be equal for all screen sizes i.e. if the width increases on iphone 7 then the height must also be the same as that of width. So you must set the aspect ratio of the button to 1:1, so that if the screen size changes then the width & height increase in same manner.
Flutter: How to create round/circular button
We can create a circular button by FloatingActionButton
widget.
return Container(
alignment: Alignment.center,
child: new SizedBox(
child: FloatingActionButton(
backgroundColor: Colors.red,
child: null,
onPressed: () {
print("Cliked");
},)
));
}
Output:
How to create a circular calculator button in Swift 5.1?
for x in 0..<3{
let buttonOnetoThree = UIButton(frame: CGRect(x: buttonSize * CGFloat(x), y: holder.frame.size.height-(buttonSize*2), width: buttonSize, height: buttonSize))
// make circle button
buttonOnetoThree.layer.cornerRadius = buttonSize/2
buttonOnetoThree.backgroundColor = .white
buttonOnetoThree.setTitle("\(x+1)", for: .normal)
holder.addSubview(buttonOnetoThree)
}
How to shrink a rectangular round cornered UIButton to a circular UIButton?
I did some tinkering and got pretty decent results.
EDIT:
I just uploaded a demo project to GitHub called MorphingButton (link) that generates the animation below:
Here's what I did:
I created a normal iOS 8 button in IB (no outline at all) and connected an outlet and an action to it.
I added height and width constraints.
I added code to set the borderColor, borderWidth, and cornerRadius of the button's layer to give it a rounded corner look. This would take some adjustment to make it look like a real rounded rectangle button.
In the IBAction for the button, switch back and forth between making it round and making it rectangular.
To make the button round:
- Use a UIView animateWithDuration method call to set the button's
height constraint to it's width constraint (making it square) and invokelayoutWithNeeded()
- Use aCABasicAnimation to animate the button's layer's corner radius to 1/2
the button width.
To make the button rectangular:
- Use a UIView animateWithDuration method call to set the button's
height constraint to it's starting height constraint - Use aCABasicAnimation to animate the button's layer's corner radius to 10 (which looks pretty good for a rounded rectangle button.)
The IBAction and viewDidLoad code would look like this in Objective-C:
- (void)viewDidLoad
{
oldHeight = buttonHeightConstraint.constant;
buttonIsRound = FALSE;
[super viewDidLoad];
animationDuration = 0.5;
}
- (IBAction)handleButton:(id)sender
{
CGFloat newHeight;
CGFloat newCornerRadius;
NSLog(@"Entering %s", __PRETTY_FUNCTION__);
if (buttonIsRound)
{
//If the button is currently round,
//go back to the old height/corner radius
newHeight = oldHeight;
newCornerRadius = 10;
}
else
{
//It isn't round now,
//so make it's height and width the same
//and set the corner radius to 1/2 the width
newHeight = buttonWidthConstraint.constant;
newCornerRadius = buttonWidthConstraint.constant/2;
}
[UIView animateWithDuration: animationDuration
animations:^
{
buttonHeightConstraint.constant = newHeight;
[button layoutIfNeeded];
}];
CABasicAnimation *cornerAnimation = [[CABasicAnimation alloc] init];
cornerAnimation.keyPath = @"cornerRadius";
cornerAnimation.fromValue = @(button.layer.cornerRadius);
cornerAnimation.toValue = @(newCornerRadius);
cornerAnimation.duration = animationDuration;
[button.layer addAnimation: cornerAnimation forKey: @"woof"];
button.layer.cornerRadius = newCornerRadius;
buttonIsRound = !buttonIsRound;
}
The Swift IBAction code for the button looks like this:
@IBAction func handleButton(sender: AnyObject)
{
if !buttonIsRound
{
UIView.animateWithDuration(animationDuration)
{
self.buttonHeightConstraint.constant = self.buttonWidthConstraint.constant
self.button.layoutIfNeeded()
self.buttonIsRound = true
}
let cornerAnimation = CABasicAnimation(keyPath: "cornerRadius")
cornerAnimation.fromValue = button.layer.cornerRadius
cornerAnimation.toValue = self.buttonWidthConstraint.constant / 2.0
cornerAnimation.duration = animationDuration
button.layer.addAnimation(cornerAnimation, forKey: "woof")
button.layer.cornerRadius = self.buttonWidthConstraint.constant / 2.0
}
else
{
UIView.animateWithDuration(animationDuration)
{
self.buttonHeightConstraint.constant = self.oldHeight
self.button.layoutIfNeeded()
self.buttonIsRound = false
}
let cornerAnimation = CABasicAnimation(keyPath: "cornerRadius")
cornerAnimation.fromValue = self.buttonWidthConstraint.constant / 2.0
cornerAnimation.toValue = 10
cornerAnimation.duration = animationDuration
button.layer.addAnimation(cornerAnimation, forKey: "woof")
button.layer.cornerRadius = 10
}
}
iOS - rectangle appears after tapping button
The style was set to System
. Setting it to Custom
fixed the problem and the rectangle is not appearing anymore. I set it from the xib (if it makes any difference)
SwiftUI scale effect from RoundedRectangle to Circle
Try this code to change rectangular button to round button.
ZStack {
// Rectangle CGFloat(self.isTapped ? 25.0 : 50)
RoundedRectangle(cornerRadius: CGFloat(self.isTapped ? 25.0 : 50))
.foregroundColor(self.isTapped ?.red :.blue)
.frame(width: CGFloat(self.isTapped ? 200 : 100), height: CGFloat(self.isTapped ? 40 : 100), alignment: .center)
Text(self.isTapped ? "Start" : "Stop")
.foregroundColor(Color.white)
}
.animation(.easeOut(duration: 0.5))
.onTapGesture {
withAnimation {
self.isTapped.toggle()
}
}
}
Circle button css
For div
tag there is already default property display:block
given by browser. For anchor tag there is not display property given by browser. You need to add display property to it. That's why use display:block
or display:inline-block
. It will work.
.btn { display:block; height: 300px; width: 300px; border-radius: 50%; border: 1px solid red; }
<a class="btn" href="#"><i class="ion-ios-arrow-down"></i></a>
Related Topics
Uiimageview .Scaleaspectfit and Autolayout Not Working Programmatically from Swift
Swift App Crashes on Real Device But Works on Simulator
How to Read References Given by Ptr_Refs in iOS
How to Get the Opposite Value of a Bool in Swift
How to Remove Single Object in Array from Multiple Matching Object
Generate a Hmac Swift Sdk8.3 Using Cchmac()
Dynamically Passing Closure with Keypaths to a Sorting Function
How to Fix Uilabel Text Spacing
Array Element Checking in Swift
Skaction Works in Didmovetoview But Doesn't Works in Function
How to Change How a Remote Notification Is Presented Before Presentation
Collectionview Flowlayout Overlapping When Scrolling Not Working
Indexing into Array of Functions: Expression Resolves to an Unused L-Value
Create Hash in Swift Using Key and Message
iOS Uiimagepickercontroller: Any Way of Getting the Date of the Chosen Picture