How to Add Uivibrancyeffect to an Existing Uilabel ( Iboutlet )

How can I make a UILabel's text style adapt to the blurred background in tvOS?

This is achievable with two nested UIVisualEffectViews. You should apply a UIBlurEffect to the parent and add a UIVibrancyEffect to the child. It pretty much works in the same way as it does on iOS.

You can achieve this in Interface Builder by using Visual Effect Views with Blur and Vibrancy, which creates the following view structure:

Visual Effect Views with Blur and Vibrancy - view structure

You can also create this effect programmatically with this approach.

Creating a blurring overlay view

You can use UIVisualEffectView to achieve this effect. This is a native API that has been fine-tuned for performance and great battery life, plus it's easy to implement.

Swift:

//only apply the blur if the user hasn't disabled transparency effects
if !UIAccessibility.isReduceTransparencyEnabled {
view.backgroundColor = .clear

let blurEffect = UIBlurEffect(style: .dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
//always fill the view
blurEffectView.frame = self.view.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

view.addSubview(blurEffectView) //if you have more UIViews, use an insertSubview API to place it where needed
} else {
view.backgroundColor = .black
}

Objective-C:

//only apply the blur if the user hasn't disabled transparency effects
if (!UIAccessibilityIsReduceTransparencyEnabled()) {
self.view.backgroundColor = [UIColor clearColor];

UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
//always fill the view
blurEffectView.frame = self.view.bounds;
blurEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

[self.view addSubview:blurEffectView]; //if you have more UIViews, use an insertSubview API to place it where needed
} else {
self.view.backgroundColor = [UIColor blackColor];
}

If you are presenting this view controller modally to blur the underlying content, you'll need to set the modal presentation style to Over Current Context and set the background color to clear to ensure the underlying view controller will remain visible once this is presented overtop.

Connect a UILabel in Interface Builder and XCode?

Assuming your view is called ExampleView. Click on the file owner and then press ⌘+4. This will highlight the identity box. Make sure that the class name is the same as the name of your class.

Save and close Interface Builder and then go into Xcode and verify:

// ExampleViewController.h
#import <UIKit/UIKit.h>

@class ExampleViewController;
@interface ExampleViewController : UIViewController {

IBOutlet UILabel *label;
}

@property (retain, nonatomic) IBOutlet UILabel *label;

@end

In your .m file:

// ExampleViewController.m
#import "ExampleViewController.h"

@implementation ExampleViewController

@synthesize label;

Then save the xcode files and open up your ExampleView. Drag a label onto the view. You are not supposed to connect that label to the Files owner.

INSTEAD YOU CLICK THE FILEOWNER. HIT ⌘+2 this will open the connections box. then you will see your outlet. Click and connect that to your label.

Is it possible to use something like an IBOutlet array?

you cannot do it in IB, but you can create an array in your init method and add all your labels to it.

BTW, you can set some tag to each label and define macro to access it. smth like

#define NAME[TAG] (UILabel*)[self.view viewWithTag:TAG]

How can I access IBOutlet in another class?

Your approach is incorrect. A view controller is initiated when it is displayed on the screen. One and only on view controller object can be displayed at one time. In your code, you are initiating a brand new view controller and set text to outlets. So that won't work. Instead, you need to set text to the text field on the existing instance of you view controller.

To do so, in the view controller that you want to receive text field content updates, register in notification center to receive a content update function calls.

NotificationCenter.default.addObserver(self, selector: #selector(listnerFunction(_:)), name: NSNotification.Name(rawValue: "notificationName"), object: nil)

func listnerFunction(_ notification: NSNotification) {
if let data = notification.userInfo?["data"] as? String {
self.textField.text = data
}
}

Then in another view controller, if you want to send text to the above view controller and update text, simply post the data to notification center

let data:[String: String] = ["data": "YourData"]
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationName"), object: nil, userInfo: data)


Related Topics



Leave a reply



Submit