Here is the picture :
I tried first with buttons, and then with UILabels on top of UIViews but the problem remained the same. Here's my code:
interView = UIView()
interView.backgroundColor = UIColor.clearColor()
interView.layer.borderWidth = 0.4
interView.layer.borderColor = UIColor.blackColor().colorWithAlphaComponent(0.4).CGColor
interView.frame = CGRectMake(0, 0, 130, 70)
interView.layer.shadowColor = UIColor.clearColor().CGColor
self.view.addSubview(interView)
So, I would like my UIView to have the same widths on every side of its border. Why is this happening?
Related
I wanted to add circle image on left navigation bar item as button. I could added, but it is not circle it is ellipse. Here is my code.
let button = UIButton();
button.downloaded(from: user?.Image);
button.frame = CGRect(x: 0, y: 0, width: 36, height:36);
button.layer.cornerRadius = button.frame.width / 2;
button.layer.masksToBounds = true;
button.imageView?.contentMode = .scaleAspectFill;
let barBtn = UIBarButtonItem(customView: button);
self.navigationItem.leftBarButtonItem = barBtn;
You can check my screen shot. Can you help me?
Issue arises when downloaded image is larger than (36, 36) it resets the frame of imageView and as result UIbutton's frame is also reset. You need to resize the image before assigning it to imageView making it smaller than (36,36).
Also don't make UIButton round, make imageView round. Otherwise touchable area of button will be reduced.
use:
button.imageView.layer.cornerRadius = button.imageView.frame.width / 2;
button.imageView.layer.masksToBounds = true;
Instead of:
button.layer.cornerRadius = button.frame.width / 2;
button.layer.masksToBounds = true;
Refer to following answer for resizing images.
How to Resize image in Swift?
I have a UILabel, that I am setting up like this:
class someSuperclass {
var firstLetterLabel = UILabel(frame: CGRect(x: 58, y: 0, width: 81, height: 120))
func commonInit() {
firstLetterLabel.font = UIFont(name: "MuseoSans-500", size: 110.0)
firstLetterLabel.textColor = UIColor.museumRed
firstLetterLabel.textAlignment = .center
firstLetterLabel.numberOfLines = 1
firstLetterLabel.lineBreakMode = .byClipping
firstLetterLabel.clipsToBounds = false
addSubview(firstLetterLabel)
}
}
But it is still being clipped by its bounds
Since clipsToBounds does not seem to apply to the content of a label. How can I stop label content from being clipped by it's frame/bounds?
ClipsToBounds allows subviews or sub layers to spill out of a view or prevent that but it does not do that for drawn content by a view and in this case it is a UILabel. You cannot draw past the bounds of the view/label. That is why it is clipped. This difference always clips drawn content.
possible solutions
1) let intrinsic size of the single letter labels keep it from clipping. Place all the the labels in a horizontal stack view.
2) enable minimum font scale on the label to allow it to fit.
3) lastly it appears it is not drawing centered. Not really sure why as you have given very little to look at.
You can use UIButton instead, and setting button.userInteractionEnabled = false.
I have an UIImageView element that I added via Interface Builder (XIB file).
I create an IBOutlet to this imageView and on awakeFromNib() method I add those 2 lines (for creating a circular imageView):
self.myImageView.layer.cornerRadius = self.myImageView.frame.size.width / 2
self.myImageView.clipsToBounds = true
The constraints to this UIImageView (via Interface Builder, NameLabel is a UIlabel with frame of: x: 27, y: 170, width: 319, height: 25) are:
Width = 77
Top space to SuperView = 88
Bottom space to NameLabel = 5
Align center X = NameLabel
When I run the app I can't see the imageView - it just disappears!
Note: (I think it happened every time a constraint sets its height).
Note 2: When I remove self.myImageView.clipsToBounds = true line or self.myImageView.layer.cornerRadius = self.myImageView.frame.size.width / 2 line it works!
Any idea how to fix it?
Thank you!
i've created a paged UIScrollView which contains labels on every page. How can i lower the margin between the labels like in the picture and then still being able to scroll?
At the moment my navigationBar look like following. Where u can swipe to next page to the right and get the next label.
What i want is something like this with a small margin and still being able to swipe.
viewDidLoad code
//Category ScrollView
categoryScrollView = UIScrollView(frame: CGRectMake(0, self.navigationController!.navigationBar.frame.height-38, self.view.frame.width, 38))
categoryScrollView?.contentSize = CGSizeMake(self.view.frame.width, 38)
categoryScrollView?.delegate = self
categoryScrollView?.pagingEnabled = true
self.navigationController?.navigationBar.addSubview(categoryScrollView!)
categoryArray = NSArray(objects: "Book", "Elektronik")
var textWidth = 0
for val in categoryArray!
{
var textLabel: UILabel = UILabel()
textLabel.textAlignment = NSTextAlignment.Center
textLabel.text = val as NSString
textLabel.frame = CGRectMake(CGFloat(textWidth), 0, categoryScrollView!.frame.width, categoryScrollView!.frame.height)
categoryScrollView?.addSubview(textLabel)
textWidth = textWidth + Int(textLabel.frame.size.width)
if textWidth > Int(self.view.frame.width) {
categoryScrollView?.contentSize = CGSizeMake(CGFloat(textWidth), categoryScrollView!.frame.height);
}
}
The default width of the "page" when pagingEnabled is switched on is the width of the scroll view. You could reduce the width and let the contained views be visible beyond the edges of the view by setting clipsToBounds to false.
If this does not work for you there also the option to adjust the paging width by intercepting the end of the drag via the UIScrollViewDelegate method scrollViewWillEndDragging(...) where you can adjust the targetContentOffset to fit your needs (see an example here).
I'm currently using a UILabel to display some text. I'm using CALayer to give it a nice shadow. But: performance is really bad when I'm positioning (and moving) a lot of UILabel elements on the screen.
I've read in a few blogposts that setting the shadowPath of a CALayer optimizes the performance, I gave it a shot with a rounded rectangle and it actually works. So now i'm trying to figure out how to get the shadowPath for a text on a UILabel.
Somebody that has an idea? Otherwise I'll have to render my text in coregraphics I guess.
Here's an example with the shadowPath on a rectangle layer:
backgroundImage.layer.shadowOffset = CGSizeMake(0, 0);
backgroundImage.layer.shadowRadius = LIST_TITLE_VIEW_SHADOW_SIZE;
backgroundImage.layer.shadowOpacity = LIST_TITLE_VIEW_SHADOW_OPACITY;
backgroundImage.layer.shadowPath = [UIBezierPath bezierPathWithRect:backgroundImage.bounds].CGPath;
I´ve found a solution that works like a charm for me. Just make an Image from the label like in this sample:
UILabel employeeName = new UILabel(new RectangleF(7, 49, this.View.Bounds.Width, 50));
employeeName.Text = "David Johnson";
employeeName.Font = UIFont.SystemFontOfSize(23);
employeeName.BackgroundColor = UIColor.Clear;
employeeName.TextColor = UIColor.White;
employeeName.Layer.ShadowColor = UIColor.Black.CGColor;
employeeName.Layer.ShadowOffset = new SizeF(1,1);
employeeName.Layer.ShadowOpacity = 10;
employeeName.Layer.ShadowRadius = 0.8f;
UIGraphics.BeginImageContextWithOptions(employeeName.Bounds.Size, false, 0);
employeeName.Layer.RenderInContext(UIGraphics.GetCurrentContext());
UIImageView employeeNameImage = new UIImageView(UIGraphics.GetImageFromCurrentImageContext());
employeeNameImage.Frame = new RectangleF(7, 49, this.View.Bounds.Width, 50);
imageView.AddSubview(employeeNameImage);