Dając UIView zaokrąglone rogi
Mój widok logowania ma subview, który ma UIActivityView
i UILabel
mówiąc " Logowanie...". Ten podgląd ma rogi, które nie są zaokrąglone. Jak mogę je zrobić okrągłe?
19 answers
Spróbuj tego
#import <QuartzCore/QuartzCore.h> // not necessary for 10 years now :)
...
view.layer.cornerRadius = 5;
view.layer.masksToBounds = true;
Uwaga: Jeśli próbujesz zastosować zaokrąglone rogi do widoku UIViewController
, nie powinno to być stosowane w konstruktorze kontrolera widoku, ale raczej w -viewDidLoad
, po tym, jak view
jest faktycznie utworzone.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-06-16 20:51:14
Możesz również użyć atrybuty Runtime zdefiniowane przez Użytkownika funkcja interface builder pozwala ustawić kluczową ścieżkę layer.cornerRadius
Na wartość. Upewnij się jednak, że dołączasz bibliotekę QuartzCore
.
Ta sztuczka działa również przy ustawianiu warstwy.borderWidth nie będzie jednak działać dla layer.borderColor
, ponieważ oczekuje CGColor
, a nie UIColor
.
Nie będzie można zobaczyć efektów w storyboardzie, ponieważ parametry te są oceniane w czasie wykonywania.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-03 18:49:07
Teraz możesz użyć kategorii swift w UIView (kod poniżej obrazka) w @IBInspectable, aby pokazać wynik na storyboardzie (jeśli używasz kategorii, używaj tylko cornerRadius, a nie warstwy.cornerRadius jako kluczowa ścieżka.
extension UIView {
@IBInspectable var cornerRadius: CGFloat {
get {
return layer.cornerRadius
}
set {
layer.cornerRadius = newValue
layer.masksToBounds = newValue > 0
}
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-01-04 20:30:53
Inne podejście niż to, które zrobił Ed Marty:
#import <QuartzCore/QuartzCore.h>
[v.layer setCornerRadius:25.0f];
[v.layer setMasksToBounds:YES];
Potrzebujesz setMasksToBounds, aby załadować wszystkie obiekty z IB... mam problem z zaokrągleniem widoku, ale nie mam obiektów z IB: /
This fixed it = d hope it helps!
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-12-24 20:29:05
Swift
Krótka odpowiedź:
myView.layer.cornerRadius = 8
myView.layer.masksToBounds = true // optional
Odpowiedź Uzupełniająca
Jeśli doszedłeś do tej odpowiedzi, prawdopodobnie już widziałeś wystarczająco dużo, aby rozwiązać swój problem. Dodaję tę odpowiedź, aby dać trochę bardziej wizualne wyjaśnienie, dlaczego rzeczy robią to, co robią.
Jeśli zaczniesz od zwykłej UIView
ma ona kwadratowe narożniki.
let blueView = UIView()
blueView.frame = CGRect(x: 100, y: 100, width: 100, height: 50)
blueView.backgroundColor = UIColor.blueColor()
view.addSubview(blueView)
Możesz nadać mu zaokrąglone rogi, zmieniając właściwość cornerRadius
widoku layer
.
blueView.layer.cornerRadius = 8
Większe wartości promienia dają bardziej zaokrąglone rogi
blueView.layer.cornerRadius = 25
I mniejsze wartości dają mniej zaokrąglone rogi.
blueView.layer.cornerRadius = 3
To może wystarczyć, by rozwiązać twój problem. Jednak czasami Widok może mieć podwarstwę lub podwarstwę wykraczającą poza granice widoku. Na przykład, gdybym miał dodać sub widok w ten sposób
let mySubView = UIView()
mySubView.frame = CGRect(x: 20, y: 20, width: 100, height: 100)
mySubView.backgroundColor = UIColor.redColor()
blueView.addSubview(mySubView)
Lub jeśli were to add a sub layer like this
let mySubLayer = CALayer()
mySubLayer.frame = CGRect(x: 20, y: 20, width: 100, height: 100)
mySubLayer.backgroundColor = UIColor.redColor().CGColor
blueView.layer.addSublayer(mySubLayer)
Wtedy skończyłbym z
Teraz, jeśli nie chcę, żeby rzeczy wisiały poza granicami, mogę to zrobić
blueView.clipsToBounds = true
Lub to
blueView.layer.masksToBounds = true
Co daje wynik:
Zarówno clipsToBounds
jak i masksToBounds
są odpowiednikami . Po prostu pierwszy jest używany z UIView
, a drugi z CALayer
.
Zobacz również
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 10:31:36
Jak opisano w ten wpis na blogu , Oto metoda na zaokrąglenie rogów UIView:
+(void)roundView:(UIView *)view onCorner:(UIRectCorner)rectCorner radius:(float)radius
{
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds
byRoundingCorners:rectCorner
cornerRadii:CGSizeMake(radius, radius)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = view.bounds;
maskLayer.path = maskPath.CGPath;
[view.layer setMask:maskLayer];
[maskLayer release];
}
Najfajniejsze jest to, że możesz wybrać, które rogi chcesz zaokrąglać.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-09-10 20:50:57
Najpierw musisz zaimportować plik nagłówkowy <QuartzCore/QuartzCore.h>
#import QuartzCore/QuartzCore.h>
[yourView.layer setCornerRadius:8.0f];
yourView.layer.borderColor = [UIColor redColor].CGColor;
yourView.layer.borderWidth = 2.0f;
[yourView.layer setMasksToBounds:YES];
Nie przegap użycia - setMasksToBounds
, w przeciwnym razie efekt może nie zostać pokazany.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-29 06:12:09
Możesz użyć następującej klasy UIView
, która może również zmieniać kolor i szerokość obramowania. Ponieważ jest to IBDesignalbe
, Możesz również zmienić atrybuty w kreatorze interfejsów.
import UIKit
@IBDesignable public class RoundedView: UIView {
@IBInspectable var borderColor: UIColor = UIColor.white {
didSet {
layer.borderColor = borderColor.cgColor
}
}
@IBInspectable var borderWidth: CGFloat = 2.0 {
didSet {
layer.borderWidth = borderWidth
}
}
@IBInspectable var cornerRadius: CGFloat = 0.0 {
didSet {
layer.cornerRadius = cornerRadius
}
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-12 02:41:18
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20, 50, 200, 200)];
view.layer.backgroundColor = [UIColor whiteColor].CGColor;
view.layer.cornerRadius = 20.0;
view.layer.frame = CGRectInset(v.layer.frame, 20, 20);
view.layer.shadowOffset = CGSizeMake(1, 0);
view.layer.shadowColor = [[UIColor blackColor] CGColor];
view.layer.shadowRadius = 5;
view.layer.shadowOpacity = .25;
[self.view addSubview:view];
[view release];
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-05-30 14:19:43
view.layer.cornerRadius = 25
view.layer.masksToBounds = true
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-16 15:35:12
Proszę zaimportować Quartzcore framework
Następnie należy ustawić setMaskToBounds
na TRUE
to bardzo ważna linia.
Następnie: [[yourView layer] setCornerRadius:5.0f];
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-29 06:12:23
UIView* viewWithRoundedCornersSize(float cornerRadius,UIView * original)
{
// Create a white border with defined width
original.layer.borderColor = [UIColor yellowColor].CGColor;
original.layer.borderWidth = 1.5;
// Set image corner radius
original.layer.cornerRadius =cornerRadius;
// To enable corners to be "clipped"
[original setClipsToBounds:YES];
return original;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-09-02 07:48:49
Swift 4-Using IBDesignable
@IBDesignable
class DesignableView: UIView {
}
extension UIView
{
@IBInspectable
var cornerRadius: CGFloat {
get {
return layer.cornerRadius
}
set {
layer.cornerRadius = newValue
}
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-24 04:34:09
Wykonaj ten program w obj c
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20, 50, 200, 200)];
view.layer.backgroundColor = [UIColor whiteColor].CGColor;
view.layer.cornerRadius = 20.0;
view.layer.frame = CGRectInset(v.layer.frame, 20, 20);
[view.layer.shadowOffset = CGSizeMake(1, 0);
view.layer.shadowColor = [[UIColor blackColor] CGColor];
view.layer.shadowRadius = 5;
view.layer.shadowOpacity = .25;][1]
[self.view addSubview:view];
Możemy to zrobić również ze stoaryboard.
layer.cornerRadius Number 5
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-21 11:39:35
Jeśli round corner nie działa w viewDidload () lepiej napisać kod w viewDidLayoutSubview()
-(void)viewDidLayoutSubviews
{
viewTextfield.layer.cornerRadius = 10.0 ;
viewTextfield.layer.borderWidth = 1.0f;
viewTextfield.layer.masksToBounds = YES;
viewTextfield.layer.shadowRadius = 5;
viewTextfield.layer.shadowOpacity = 0.3;
viewTextfield.clipsToBounds = NO;
viewTextfield.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
}
Mam nadzieję, że to pomoże!Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-21 06:22:59
Możesz również użyć obrazka:
UIImage *maskingImage = [UIImage imageNamed:@"bannerBarBottomMask.png"];
CALayer *maskingLayer = [CALayer layer];
maskingLayer.frame = CGRectMake(-(self.yourView.frame.size.width - self.yourView.frame.size.width) / 2
, 0
, maskingImage.size.width
, maskingImage.size.height);
[maskingLayer setContents:(id)[maskingImage CGImage]];
[self.yourView.layer setMask:maskingLayer];
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-01-24 06:59:09
Ustaw właściwość round View
Ustawiona wartość logiczna masksToBounds dla obrazu nie będzie nadal rysowana poza granicami promienia narożnika
view.layer.cornerRadius = 5;
view.layer.masksToBounds = YES;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-12 01:41:17
Używanie Rozszerzenia UIView:
extension UIView {
func addRoundedCornerToView(targetView : UIView?)
{
//UIView Corner Radius
targetView!.layer.cornerRadius = 5.0;
targetView!.layer.masksToBounds = true
//UIView Set up boarder
targetView!.layer.borderColor = UIColor.yellowColor().CGColor;
targetView!.layer.borderWidth = 3.0;
//UIView Drop shadow
targetView!.layer.shadowColor = UIColor.darkGrayColor().CGColor;
targetView!.layer.shadowOffset = CGSizeMake(2.0, 2.0)
targetView!.layer.shadowOpacity = 1.0
}
}
Użycie:
override func viewWillAppear(animated: Bool) {
sampleView.addRoundedCornerToView(statusBarView)
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-11 06:24:32
Na Xcode 6 Twoja próba
self.layer.layer.cornerRadius = 5.0f;
Lub
self.layer.layer.cornerRadius = 5.0f;
self.layer.clipsToBounds = YES;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-11-28 12:42:37