Take a look round after the self-timer

I have a tableview with custom cells that are built from a storyboard with an id using AutoLayout .

One of the subviews should be round (layer.cornerRadius = width/2) , this is the square at the beginning.

I tried in layoutSubviews() , but it seems to be called before AutoLayout it ... the same for didMoveToSuperview()

Where is the appropriate function for updating things like this in my subtitles after AutoLayout resized them?

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("cell_small") as! Cell ... return cell } // In Cell override func layoutSubviews() { rankLabel.layer.cornerRadius = rankLabel.bounds.width/2 rankLabel.layer.masksToBounds = true } override func didMoveToSuperview() { rankLabel.layer.cornerRadius = rankLabel.bounds.width/2 rankLabel.layer.masksToBounds = true } 

Result:

+6
source share
6 answers

As a result, I created a class called RoundView

 class RoundView:UIView { override func layoutSubviews() { super.layoutSubviews() self.layer.cornerRadius = self.bounds.width/2 self.layer.masksToBounds = true } } 

And then I apply it to all representations that need to be rounded. So in Storyboard, I add RoundView to Custom Class .

What happened is that if you look inside the storyboard source (XML), each view was the size of the entire screen, you can look inside your own SB code. Therefore, trying to add a corner radius equal to width/2 inside its parent layoutSubviews() element, subview does not have the correct layoutSubviews() . Thus, the radius radius got the value 320/2 instead of 50/2 , which is why he got the deformation.

+10
source

1.Create your own UIView / category class

 #import <UIKit/UIKit.h> @interface RoundView : UIView @end #import "RoundView.h" 

2. Add a layoutSubviews method and set the angular radius.

 @implementation RoundView -(void)layoutSubviews{ [super layoutSubviews]; self.layer.cornerRadius = self.bounds.size.width/2; self.layer.masksToBounds = true; } 

3. Make your UIView as a subclass of RoundView and run the application, you can see the circle.

+3
source

Try to subclass UITableViewCell like this,

 @interface RoundingCell : UITableViewCell @property (nonatomic,weak) IBOutlet UILabel * someLabel; @end @implementation RoundingCell -(void)layoutSubviews { [super layoutSubviews]; self.someLabel.layer.cornerRadius = CGRectGetHeight(self.someLabel.bounds)/2; self.someLabel.layer.masksToBounds = YES; } @end 

And use this as the class of the required cell, as well as the IBOutlet join.

+1
source

I had the same problem with my image, so I solved it by subclassing UIImageView.

 @interface MyImageView : UIImageView @end 

 @implementation MyImageView -(void)layoutSubviews { [super layoutSubviews]; self.layer.cornerRadius = self.bounds.size.width / 2.0; } @end 
+1
source

Strange, I only make round cells using your code. (and I also use a machine gun).

The only difference is what I use. frame and not. bounds (divided by 2). Have you tried this?

Otherwise, you can use a custom cell, and in awakeFromNib set rounding in the same way, so you do not need to do this in every cell.

0
source

You have two options:

  • Create your own YourCell class and add your code to the initWithCoder method
  • Add Method to ViewController

as below

 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 

and change it inside. To access the cell you are calling

 [(YourCell *)cell view].... 
-1
source

Source: https://habr.com/ru/post/987572/


All Articles